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 3, 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;
59 * Handle to hostlist server's disconnect handler
61 static GNUNET_CORE_DisconnectEventHandler server_dh;
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;
93 * Handle to hostlist client's connect handler
95 static GNUNET_CORE_ConnectEventHandler client_ch;
98 * Handle to hostlist client's disconnect handler
100 static GNUNET_CORE_DisconnectEventHandler client_dh;
103 * A HOSTLIST_ADV message is used to exchange information about
104 * hostlist advertisements. This struct is always
105 * followed by the actual url under which the hostlist can be obtained:
107 * 1) transport-name (0-terminated)
108 * 2) address-length (uint32_t, network byte order; possibly
110 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
112 * 4) address (address-length bytes; possibly unaligned!)
114 struct GNUNET_HOSTLIST_ADV_Message
117 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
119 struct GNUNET_MessageHeader header;
122 * Always zero (for alignment).
124 uint32_t reserved GNUNET_PACKED;
128 static struct GNUNET_PeerIdentity me;
131 core_init (void *cls, struct GNUNET_CORE_Handle *server,
132 const struct GNUNET_PeerIdentity *my_identity)
138 * Core handler for p2p hostlist advertisements
141 * @param peer identity of the sender
142 * @param message advertisement message we got
143 * @param atsi performance information
144 * @param atsi_count number of records in 'atsi'
145 * @return GNUNET_OK on success
148 advertisement_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
149 const struct GNUNET_MessageHeader *message,
150 const struct GNUNET_ATS_Information *atsi,
151 unsigned int atsi_count)
153 GNUNET_assert (NULL != client_adv_handler);
154 return (*client_adv_handler) (cls, peer, message, atsi, atsi_count);
159 * Method called whenever a given peer connects. Wrapper to call both client's and server's functions
162 * @param peer peer identity this notification is about
163 * @param atsi performance data
164 * @param atsi_count number of records in 'atsi'
167 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
168 const struct GNUNET_ATS_Information *atsi,
169 unsigned int atsi_count)
171 if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "A new peer connected, notifying client and server\n");
175 if (NULL != client_ch)
176 (*client_ch) (cls, peer, atsi, atsi_count);
178 if (NULL != server_ch)
179 (*server_ch) (cls, peer, atsi, atsi_count);
184 * Method called whenever a given peer disconnects. Wrapper to call both client's and server's functions
187 * @param peer peer identity this notification is about
190 disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
192 if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
194 /* call hostlist client disconnect handler */
195 if (NULL != client_dh)
196 (*client_dh) (cls, peer);
198 /* call hostlist server disconnect handler */
199 if (NULL != server_dh)
200 (*server_dh) (cls, peer);
205 * Last task run during shutdown. Disconnects us from
206 * the other services.
209 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist daemon is shutting down\n");
214 GNUNET_CORE_disconnect (core);
219 GNUNET_HOSTLIST_client_stop ();
222 if (provide_hostlist)
224 GNUNET_HOSTLIST_server_stop ();
229 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
236 * Main function that will be run.
239 * @param args remaining command-line arguments
240 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
241 * @param cfg configuration
244 run (void *cls, char *const *args, const char *cfgfile,
245 const struct GNUNET_CONFIGURATION_Handle *cfg)
247 static const struct GNUNET_CORE_MessageHandler learn_handlers[] = {
248 {&advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
251 static const struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
254 if ((!bootstrapping) && (!learning)
256 && (!provide_hostlist)
260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
262 ("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
268 stats = GNUNET_STATISTICS_create ("hostlist", cfg);
271 GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_handler,
272 &disconnect_handler, NULL, GNUNET_NO, NULL,
274 learning ? learn_handlers : no_learn_handlers);
278 GNUNET_HOSTLIST_client_start (cfg, stats, &client_ch, &client_dh,
279 &client_adv_handler, learning);
283 if (provide_hostlist)
285 GNUNET_HOSTLIST_server_start (cfg, stats, core, &server_ch, &server_dh,
289 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 _("Failed to connect to `%s' service.\n"), "core");
296 GNUNET_SCHEDULER_shutdown ();
303 * The main function for the hostlist daemon.
305 * @param argc number of arguments from the command line
306 * @param argv command line arguments
307 * @return 0 ok, 1 on error
310 main (int argc, char *const *argv)
312 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
314 {'a', "advertise", NULL,
315 gettext_noop ("advertise our hostlist to other peers"),
316 GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising},
318 {'b', "bootstrap", NULL,
320 ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
321 GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping},
322 {'e', "enable-learning", NULL,
323 gettext_noop ("enable learning about hostlist servers from other peers"),
324 GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
326 {'p', "provide-hostlist", NULL,
327 gettext_noop ("provide a hostlist server"),
328 GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
330 GNUNET_GETOPT_OPTION_END
335 GNUNET_log_setup ("hostlist", "WARNING", NULL);
338 GNUNET_PROGRAM_run (argc, argv, "hostlist",
339 _("GNUnet hostlist server and client"), options,
340 &run, NULL)) ? 0 : 1;
345 /* end of gnunet-daemon-hostlist.c */