2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009, 2014 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
27 #include "gnunet-daemon-hostlist_client.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_statistics_service.h"
35 #include "gnunet-daemon-hostlist_server.h"
38 * Set if we are allowed to advertise our hostlist to others.
40 static int advertising;
43 * Set if the user wants us to run a hostlist server.
45 static int provide_hostlist;
48 * Handle to hostlist server's connect handler
50 static GNUNET_CORE_ConnectEventHandler server_ch;
53 * Handle to hostlist server's disconnect handler
55 static GNUNET_CORE_DisconnectEventHandler server_dh;
60 * Set if we are allowed to learn about peers by accessing
63 static int bootstrapping;
66 * Set if the user allows us to learn about new hostlists
74 static struct GNUNET_STATISTICS_Handle *stats;
77 * Handle to the core service (NULL until we've connected to it).
79 static struct GNUNET_CORE_Handle *core;
82 * Handle to the hostlist client's advertisement handler
84 static GNUNET_CORE_MessageCallback client_adv_handler;
87 * Handle to hostlist client's connect handler
89 static GNUNET_CORE_ConnectEventHandler client_ch;
92 * Handle to hostlist client's disconnect handler
94 static GNUNET_CORE_DisconnectEventHandler client_dh;
96 GNUNET_NETWORK_STRUCT_BEGIN
99 * A HOSTLIST_ADV message is used to exchange information about
100 * hostlist advertisements. This struct is always
101 * followed by the actual url under which the hostlist can be obtained:
103 * 1) transport-name (0-terminated)
104 * 2) address-length (uint32_t, network byte order; possibly
106 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
108 * 4) address (address-length bytes; possibly unaligned!)
110 struct GNUNET_HOSTLIST_ADV_Message
113 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
115 struct GNUNET_MessageHeader header;
118 * Always zero (for alignment).
120 uint32_t reserved GNUNET_PACKED;
122 GNUNET_NETWORK_STRUCT_END
126 * Our own peer identity.
128 static struct GNUNET_PeerIdentity me;
132 * Callback invoked once our connection to CORE service is up.
135 * @param my_identity our peer's identity
138 core_init (void *cls,
139 const struct GNUNET_PeerIdentity *my_identity)
146 * Core handler for p2p hostlist advertisements
149 * @param peer identity of the sender
150 * @param message advertisement message we got
151 * @return #GNUNET_OK on success
154 advertisement_handler (void *cls,
155 const struct GNUNET_PeerIdentity *peer,
156 const struct GNUNET_MessageHeader *message)
158 GNUNET_assert (NULL != client_adv_handler);
159 return (*client_adv_handler) (cls, peer, message);
164 * Method called whenever a given peer connects. Wrapper to call both
165 * client's and server's functions
168 * @param peer peer identity this notification is about
171 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
173 if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "A new peer connected, notifying client and server\n");
177 if (NULL != client_ch)
178 (*client_ch) (cls, peer);
180 if (NULL != server_ch)
181 (*server_ch) (cls, peer);
187 * Method called whenever a given peer disconnects. Wrapper to call
188 * both client's and server's functions
191 * @param peer peer identity this notification is about
194 disconnect_handler (void *cls,
195 const struct GNUNET_PeerIdentity *peer)
197 if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
199 /* call hostlist client disconnect handler */
200 if (NULL != client_dh)
201 (*client_dh) (cls, peer);
203 /* call hostlist server disconnect handler */
204 if (NULL != server_dh)
205 (*server_dh) (cls, peer);
211 * Last task run during shutdown. Disconnects us from
212 * the other services.
215 * @param tc scheduler context
218 cleaning_task (void *cls,
219 const struct GNUNET_SCHEDULER_TaskContext *tc)
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Hostlist daemon is shutting down\n");
225 GNUNET_CORE_disconnect (core);
230 GNUNET_HOSTLIST_client_stop ();
233 if (provide_hostlist)
235 GNUNET_HOSTLIST_server_stop ();
240 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
247 * Main function that will be run.
250 * @param args remaining command-line arguments
251 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
252 * @param cfg configuration
258 const struct GNUNET_CONFIGURATION_Handle *cfg)
260 static const struct GNUNET_CORE_MessageHandler learn_handlers[] = {
261 {&advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
264 static const struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
267 if ((! bootstrapping) && (! learning)
269 && (! provide_hostlist)
273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
274 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
277 stats = GNUNET_STATISTICS_create ("hostlist", cfg);
279 GNUNET_HOSTLIST_client_start (cfg, stats, &client_ch, &client_dh,
280 &client_adv_handler, learning);
282 GNUNET_CORE_connect (cfg, NULL,
285 &disconnect_handler, NULL,
288 learning ? learn_handlers : no_learn_handlers);
292 if (provide_hostlist)
293 GNUNET_HOSTLIST_server_start (cfg, stats, core, &server_ch, &server_dh,
296 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 _("Failed to connect to `%s' service.\n"), "core");
304 GNUNET_SCHEDULER_shutdown ();
311 * The main function for the hostlist daemon.
313 * @param argc number of arguments from the command line
314 * @param argv command line arguments
315 * @return 0 ok, 1 on error
318 main (int argc, char *const *argv)
320 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
322 {'a', "advertise", NULL,
323 gettext_noop ("advertise our hostlist to other peers"),
324 GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising},
326 {'b', "bootstrap", NULL,
328 ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
329 GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping},
330 {'e', "enable-learning", NULL,
331 gettext_noop ("enable learning about hostlist servers from other peers"),
332 GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
334 {'p', "provide-hostlist", NULL,
335 gettext_noop ("provide a hostlist server"),
336 GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
338 GNUNET_GETOPT_OPTION_END
343 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
346 GNUNET_log_setup ("hostlist", "WARNING", NULL);
349 GNUNET_PROGRAM_run (argc, argv, "hostlist",
350 _("GNUnet hostlist server and client"),
352 &run, NULL)) ? 0 : 1;
353 GNUNET_free ((void*) argv);
357 /* end of gnunet-daemon-hostlist.c */