2 This file is part of GNUnet.
3 Copyright (C) 2007, 2008, 2009, 2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
55 * Set if we are allowed to learn about peers by accessing
58 static int bootstrapping;
61 * Set if the user allows us to learn about new hostlists
69 static struct GNUNET_STATISTICS_Handle *stats;
72 * Handle to the core service (NULL until we've connected to it).
74 static struct GNUNET_CORE_Handle *core;
77 * Handle to the hostlist client's advertisement handler
79 static GNUNET_HOSTLIST_UriHandler client_adv_handler;
82 * Handle to hostlist client's connect handler
84 static GNUNET_CORE_ConnectEventHandler client_ch;
87 * Handle to hostlist client's disconnect handler
89 static GNUNET_CORE_DisconnectEventHandler client_dh;
91 GNUNET_NETWORK_STRUCT_BEGIN
94 * A HOSTLIST_ADV message is used to exchange information about
95 * hostlist advertisements. This struct is always
96 * followed by the actual url under which the hostlist can be obtained:
98 * 1) transport-name (0-terminated)
99 * 2) address-length (uint32_t, network byte order; possibly
101 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
103 * 4) address (address-length bytes; possibly unaligned!)
105 struct GNUNET_HOSTLIST_ADV_Message
108 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
110 struct GNUNET_MessageHeader header;
113 * Always zero (for alignment).
115 uint32_t reserved GNUNET_PACKED;
117 GNUNET_NETWORK_STRUCT_END
121 * Our own peer identity.
123 static struct GNUNET_PeerIdentity me;
127 * Callback invoked once our connection to CORE service is up.
130 * @param my_identity our peer's identity
133 core_init (void *cls,
134 const struct GNUNET_PeerIdentity *my_identity)
141 * Core handler for p2p hostlist advertisements
144 * @param message advertisement message we got
145 * @return #GNUNET_OK if message is well-formed
148 check_advertisement (void *cls,
149 const struct GNUNET_MessageHeader *message)
155 size = ntohs (message->size);
156 if (size <= sizeof(struct GNUNET_MessageHeader))
159 return GNUNET_SYSERR;
161 uri = (const char *) &message[1];
162 uri_size = size - sizeof(struct GNUNET_MessageHeader);
163 if (uri[uri_size - 1] != '\0')
166 return GNUNET_SYSERR;
173 * Core handler for p2p hostlist advertisements
176 * @param message advertisement message we got
177 * @return #GNUNET_OK on success
180 handle_advertisement (void *cls,
181 const struct GNUNET_MessageHeader *message)
183 const char *uri = (const char *) &message[1];
185 GNUNET_assert (NULL != client_adv_handler);
186 (void) (*client_adv_handler)(uri);
191 * Method called whenever a given peer connects. Wrapper to call both
192 * client's and server's functions
195 * @param peer peer identity this notification is about
196 * @param mq queue for sending messages to @a peer
200 connect_handler (void *cls,
201 const struct GNUNET_PeerIdentity *peer,
202 struct GNUNET_MQ_Handle *mq)
204 if (0 == GNUNET_memcmp (&me,
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "A new peer connected, notifying client and server\n");
209 if (NULL != client_ch)
210 GNUNET_assert (NULL ==
215 if (NULL != server_ch)
216 GNUNET_assert (NULL ==
221 return (void *) peer;
226 * Method called whenever a given peer disconnects. Wrapper to call
227 * both client's and server's functions
230 * @param peer peer identity this notification is about
233 disconnect_handler (void *cls,
234 const struct GNUNET_PeerIdentity *peer,
237 if (0 == GNUNET_memcmp (&me,
240 /* call hostlist client disconnect handler */
241 if (NULL != client_dh)
249 * Last task run during shutdown. Disconnects us from
250 * the other services.
255 cleaning_task (void *cls)
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Hostlist daemon is shutting down\n");
261 GNUNET_CORE_disconnect (core);
266 GNUNET_HOSTLIST_client_stop ();
269 if (provide_hostlist)
271 GNUNET_HOSTLIST_server_stop ();
276 GNUNET_STATISTICS_destroy (stats,
284 * Main function that will be run.
287 * @param args remaining command-line arguments
288 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
289 * @param cfg configuration
295 const struct GNUNET_CONFIGURATION_Handle *cfg)
297 struct GNUNET_MQ_MessageHandler learn_handlers[] = {
298 GNUNET_MQ_hd_var_size (advertisement,
299 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT,
300 struct GNUNET_MessageHeader,
302 GNUNET_MQ_handler_end ()
304 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
305 GNUNET_MQ_handler_end ()
308 if ((! bootstrapping) && (! learning)
310 && (! provide_hostlist)
314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
316 "None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
319 stats = GNUNET_STATISTICS_create ("hostlist", cfg);
326 GNUNET_HOSTLIST_client_start (cfg,
333 GNUNET_CORE_connect (cfg,
338 learning ? learn_handlers : no_learn_handlers);
342 if (provide_hostlist)
343 GNUNET_HOSTLIST_server_start (cfg,
349 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 _ ("Failed to connect to `%s' service.\n"), "core");
356 GNUNET_SCHEDULER_shutdown ();
363 * The main function for the hostlist daemon.
365 * @param argc number of arguments from the command line
366 * @param argv command line arguments
367 * @return 0 ok, 1 on error
370 main (int argc, char *const *argv)
372 struct GNUNET_GETOPT_CommandLineOption options[] = {
374 GNUNET_GETOPT_option_flag ('a',
377 "advertise our hostlist to other peers"),
380 GNUNET_GETOPT_option_flag ('b',
383 "bootstrap using hostlists (it is highly recommended that you always use this option)"),
385 GNUNET_GETOPT_option_flag ('e',
388 "enable learning about hostlist servers from other peers"),
391 GNUNET_GETOPT_option_flag ('p',
393 gettext_noop ("provide a hostlist server"),
396 GNUNET_GETOPT_OPTION_END
401 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
404 GNUNET_log_setup ("hostlist", "WARNING", NULL);
407 GNUNET_PROGRAM_run (argc, argv,
409 _ ("GNUnet hostlist server and client"),
411 &run, NULL)) ? 0 : 1;
412 GNUNET_free ((void*) argv);
416 /* end of gnunet-daemon-hostlist.c */