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
29 #include "hostlist-client.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_strings_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_util_lib.h"
41 #include "hostlist-server.h"
44 * Set if we are allowed to advertise our hostlist to others.
46 static int advertising;
49 * Set if the user wants us to run a hostlist server.
51 static int provide_hostlist;
54 * Handle to hostlist server's connect handler
56 static GNUNET_CORE_ConnectEventHandler server_ch = NULL;
59 * Handle to hostlist server's disconnect handler
61 static GNUNET_CORE_DisconnectEventHandler server_dh = NULL;
66 * Set if we are allowed to learn about peers by accessing
69 static int bootstrapping;
72 * Set if the user allows us to learn about new hostlists
80 static struct GNUNET_STATISTICS_Handle *stats;
83 * Handle to the core service (NULL until we've connected to it).
85 static struct GNUNET_CORE_Handle *core;
88 * Handle to the hostlist client's advertisement handler
90 static GNUNET_CORE_MessageCallback client_adv_handler = NULL;
93 * Handle to hostlist client's connect handler
95 static GNUNET_CORE_ConnectEventHandler client_ch = NULL;
98 * Handle to hostlist client's disconnect handler
100 static GNUNET_CORE_DisconnectEventHandler client_dh = NULL;
103 * gnunet-daemon-hostlist command line options.
105 static struct GNUNET_GETOPT_CommandLineOption options[] = {
107 { 'a', "advertise", NULL,
108 gettext_noop ("advertise our hostlist to other peers"),
109 GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
111 { 'b', "bootstrap", NULL,
112 gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
113 GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
114 { 'e', "enable-learning", NULL,
115 gettext_noop ("enable learning about hostlist servers from other peers"),
116 GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
118 { 'p', "provide-hostlist", NULL,
119 gettext_noop ("provide a hostlist server"),
120 GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
122 GNUNET_GETOPT_OPTION_END
126 * A HOSTLIST_ADV message is used to exchange information about
127 * hostlist advertisements. This struct is always
128 * followed by the actual url under which the hostlist can be obtained:
130 * 1) transport-name (0-terminated)
131 * 2) address-length (uint32_t, network byte order; possibly
133 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
135 * 4) address (address-length bytes; possibly unaligned!)
137 struct GNUNET_HOSTLIST_ADV_Message
140 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
142 struct GNUNET_MessageHeader header;
145 * Always zero (for alignment).
147 uint32_t reserved GNUNET_PACKED;
152 core_init (void *cls,
153 struct GNUNET_CORE_Handle * server,
154 const struct GNUNET_PeerIdentity *
157 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
160 /* Nothing to do here */
164 * Core handler for p2p hostlist advertisements
166 static int advertisement_handler (void *cls,
167 const struct GNUNET_PeerIdentity * peer,
168 const struct GNUNET_MessageHeader * message,
169 struct GNUNET_TIME_Relative latency,
172 GNUNET_assert (NULL != client_adv_handler);
173 return (*client_adv_handler) (cls, peer, message, latency, distance);
178 * Method called whenever a given peer connects. Wrapper to call both client's and server's functions
181 * @param peer peer identity this notification is about
182 * @param latency reported latency of the connection with 'other'
183 * @param distance reported distance (DV) to 'other'
186 connect_handler (void *cls,
188 GNUNET_PeerIdentity * peer,
189 struct GNUNET_TIME_Relative latency,
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "A new peer connected, notifying client and server\n");
194 if ( NULL != client_ch)
195 (*client_ch) (cls, peer, latency, distance);
197 if ( NULL != server_ch)
198 (*server_ch) (cls, peer, latency, distance);
203 * Method called whenever a given peer disconnects. Wrapper to call both client's and server's functions
206 * @param peer peer identity this notification is about
209 disconnect_handler (void *cls,
211 GNUNET_PeerIdentity * peer)
214 /* call hostlist client disconnect handler*/
215 if ( NULL != client_dh)
216 (*client_dh) (cls, peer);
218 /* call hostlist server disconnect handler*/
219 if ( NULL != server_dh)
220 (*server_dh) (cls, peer);
225 * Last task run during shutdown. Disconnects us from
226 * the other services.
229 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Hostlist daemon is shutting down\n");
235 GNUNET_HOSTLIST_client_stop ();
238 if (provide_hostlist)
240 GNUNET_HOSTLIST_server_stop ();
245 GNUNET_CORE_disconnect (core);
250 GNUNET_STATISTICS_destroy (stats,
257 * List of handlers if we are learning.
259 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
260 { &advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
266 * List of handlers if we are not learning.
268 static struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
274 * Main function that will be run.
277 * @param sched the scheduler to use
278 * @param args remaining command-line arguments
279 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
280 * @param cfg configuration
284 struct GNUNET_SCHEDULER_Handle * sched,
287 const struct GNUNET_CONFIGURATION_Handle * cfg)
289 if ( (! bootstrapping) &&
292 && (! provide_hostlist)
296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
297 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
300 stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
302 core = GNUNET_CORE_connect (sched, cfg,
303 GNUNET_TIME_UNIT_FOREVER_REL,
306 &connect_handler, &disconnect_handler,
309 learning? learn_handlers : no_learn_handlers);
313 GNUNET_HOSTLIST_client_start (cfg, sched, stats,
314 &client_ch, &client_dh, &client_adv_handler, learning);
317 if (provide_hostlist)
319 GNUNET_HOSTLIST_server_start (cfg, sched, stats, core, &server_ch, &server_dh, advertising );
322 GNUNET_SCHEDULER_add_delayed (sched,
323 GNUNET_TIME_UNIT_FOREVER_REL,
324 &cleaning_task, NULL);
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 _("Failed to connect to `%s' service.\n"),
330 GNUNET_SCHEDULER_shutdown (sched);
337 * The main function for the hostlist daemon.
339 * @param argc number of arguments from the command line
340 * @param argv command line arguments
341 * @return 0 ok, 1 on error
344 main (int argc, char *const *argv)
347 GNUNET_log_setup ("hostlist","DEBUG",NULL);
350 GNUNET_PROGRAM_run (argc,
353 _("GNUnet hostlist server and client"),
355 &run, NULL)) ? 0 : 1;
360 /* end of gnunet-daemon-hostlist.c */