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 static struct GNUNET_CORE_Handle *core;
78 * Handle to the hostlist client's advertisement handler
80 static GNUNET_CORE_MessageCallback client_adv_handler = NULL;
83 * Handle to hostlist client's connect handler
85 static GNUNET_CORE_ConnectEventHandler client_ch = NULL;
88 * Handle to hostlist client's disconnect handler
90 static GNUNET_CORE_DisconnectEventHandler client_dh = NULL;
93 * Handle to hostlist server's connect handler
95 static GNUNET_CORE_ConnectEventHandler server_ch = NULL;
98 * Handle to hostlist server's disconnect handler
100 static GNUNET_CORE_DisconnectEventHandler server_dh = NULL;
103 * gnunet-daemon-hostlist command line options.
105 static struct GNUNET_GETOPT_CommandLineOption options[] = {
106 { 'a', "advertise", NULL,
107 gettext_noop ("advertise our hostlist to other peers"),
108 GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
109 { 'b', "bootstrap", NULL,
110 gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
111 GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
112 { 'e', "enable-learning", NULL,
113 gettext_noop ("enable learning about hostlist servers from other peers"),
114 GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
115 { 'p', "provide-hostlist", NULL,
116 gettext_noop ("provide a hostlist server"),
117 GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
118 GNUNET_GETOPT_OPTION_END
122 * A HOSTLIST_ADV message is used to exchange information about
123 * hostlist advertisements. This struct is always
124 * followed by the actual url under which the hostlist can be obtained:
126 * 1) transport-name (0-terminated)
127 * 2) address-length (uint32_t, network byte order; possibly
129 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
131 * 4) address (address-length bytes; possibly unaligned!)
133 struct GNUNET_HOSTLIST_ADV_Message
136 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
138 struct GNUNET_MessageHeader header;
141 * Always zero (for alignment).
143 uint32_t reserved GNUNET_PACKED;
148 core_init (void *cls,
149 struct GNUNET_CORE_Handle * server,
150 const struct GNUNET_PeerIdentity *
153 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
156 if (advertising && (NULL != server))
158 /* FIXME: provide "server" to 'hostlist' module */
163 * Core handler for p2p hostlist advertisements
165 static int advertisement_handler (void *cls,
166 const struct GNUNET_PeerIdentity * peer,
167 const struct GNUNET_MessageHeader * message,
168 struct GNUNET_TIME_Relative latency,
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Recieved hostlist advertisement, but I am not learning!\n");
178 if (learning && (NULL != client_adv_handler))
180 (*client_adv_handler) (cls, peer, message, latency, distance);
187 * Method called whenever a given peer connects. Wrapper to call both client's and server's functions
190 * @param peer peer identity this notification is about
191 * @param latency reported latency of the connection with 'other'
192 * @param distance reported distance (DV) to 'other'
195 connect_handler (void *cls,
197 GNUNET_PeerIdentity * peer,
198 struct GNUNET_TIME_Relative latency,
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "A new peer connected, notifying client and server\n");
203 if ( NULL != client_ch)
204 (*client_ch) (cls, peer, latency, distance);
205 if ( NULL != server_ch)
206 (*server_ch) (cls, peer, latency, distance);
210 * Method called whenever a given peer disconnects. Wrapper to call both client's and server's functions
213 * @param peer peer identity this notification is about
214 * @param latency reported latency of the connection with 'other'
215 * @param distance reported distance (DV) to 'other'
218 disconnect_handler (void *cls,
220 GNUNET_PeerIdentity * peer)
223 /* call hostlist client disconnect handler*/
224 if ( NULL != client_dh)
225 (*client_dh) (cls, peer);
227 /* call hostlist server disconnect handler*/
228 if ( NULL != server_dh)
229 (*server_dh) (cls, peer);
233 * Last task run during shutdown. Disconnects us from
234 * the other services.
237 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Hostlist daemon is shutting down\n");
243 GNUNET_HOSTLIST_client_stop ();
245 if (provide_hostlist)
247 GNUNET_HOSTLIST_server_stop ();
251 GNUNET_CORE_disconnect (core);
256 GNUNET_STATISTICS_destroy (stats,
263 * List of handlers for the messages understood by this
266 static struct GNUNET_CORE_MessageHandler handlers[] = {
267 { &advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
272 * Main function that will be run.
275 * @param sched the scheduler to use
276 * @param args remaining command-line arguments
277 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
278 * @param cfg configuration
282 struct GNUNET_SCHEDULER_Handle * sched,
285 const struct GNUNET_CONFIGURATION_Handle * cfg)
287 if ( (! bootstrapping) &&
289 (! provide_hostlist) )
291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
292 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
295 stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
297 core = GNUNET_CORE_connect (sched, cfg,
298 GNUNET_TIME_UNIT_FOREVER_REL,
301 NULL, &connect_handler, &disconnect_handler,
308 GNUNET_HOSTLIST_client_start (cfg, sched, stats,
309 &client_ch, &client_dh, &client_adv_handler);
311 if (provide_hostlist)
313 GNUNET_HOSTLIST_server_start (cfg, sched, stats, core, &server_ch, &server_dh, advertising );
320 GNUNET_SCHEDULER_add_delayed (sched,
321 GNUNET_TIME_UNIT_FOREVER_REL,
322 &cleaning_task, NULL);
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 _("Failed to connect to `%s' service.\n"),
328 GNUNET_SCHEDULER_shutdown (sched);
335 * The main function for the hostlist daemon.
337 * @param argc number of arguments from the command line
338 * @param argv command line arguments
339 * @return 0 ok, 1 on error
342 main (int argc, char *const *argv)
345 GNUNET_log_setup ("hostlist","DEBUG",NULL);
348 GNUNET_PROGRAM_run (argc,
351 _("GNUnet hostlist server and client"),
353 &run, NULL)) ? 0 : 1;
358 /* end of gnunet-daemon-hostlist.c */