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 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.
17 * @file hostlist/gnunet-daemon-hostlist.c
18 * @brief code for bootstrapping via hostlist servers
19 * @author Christian Grothoff
22 #include "gnunet-daemon-hostlist_client.h"
23 #include "gnunet_core_service.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_protocols.h"
26 #include "gnunet_statistics_service.h"
30 #include "gnunet-daemon-hostlist_server.h"
33 * Set if we are allowed to advertise our hostlist to others.
35 static int advertising;
38 * Set if the user wants us to run a hostlist server.
40 static int provide_hostlist;
43 * Handle to hostlist server's connect handler
45 static GNUNET_CORE_ConnectEventHandler server_ch;
50 * Set if we are allowed to learn about peers by accessing
53 static int bootstrapping;
56 * Set if the user allows us to learn about new hostlists
64 static struct GNUNET_STATISTICS_Handle *stats;
67 * Handle to the core service (NULL until we've connected to it).
69 static struct GNUNET_CORE_Handle *core;
72 * Handle to the hostlist client's advertisement handler
74 static GNUNET_HOSTLIST_UriHandler client_adv_handler;
77 * Handle to hostlist client's connect handler
79 static GNUNET_CORE_ConnectEventHandler client_ch;
82 * Handle to hostlist client's disconnect handler
84 static GNUNET_CORE_DisconnectEventHandler client_dh;
86 GNUNET_NETWORK_STRUCT_BEGIN
89 * A HOSTLIST_ADV message is used to exchange information about
90 * hostlist advertisements. This struct is always
91 * followed by the actual url under which the hostlist can be obtained:
93 * 1) transport-name (0-terminated)
94 * 2) address-length (uint32_t, network byte order; possibly
96 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
98 * 4) address (address-length bytes; possibly unaligned!)
100 struct GNUNET_HOSTLIST_ADV_Message
103 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
105 struct GNUNET_MessageHeader header;
108 * Always zero (for alignment).
110 uint32_t reserved GNUNET_PACKED;
112 GNUNET_NETWORK_STRUCT_END
116 * Our own peer identity.
118 static struct GNUNET_PeerIdentity me;
122 * Callback invoked once our connection to CORE service is up.
125 * @param my_identity our peer's identity
128 core_init (void *cls,
129 const struct GNUNET_PeerIdentity *my_identity)
136 * Core handler for p2p hostlist advertisements
139 * @param message advertisement message we got
140 * @return #GNUNET_OK if message is well-formed
143 check_advertisement (void *cls,
144 const struct GNUNET_MessageHeader *message)
150 size = ntohs (message->size);
151 if (size <= sizeof (struct GNUNET_MessageHeader))
154 return GNUNET_SYSERR;
156 uri = (const char *) &message[1];
157 uri_size = size - sizeof (struct GNUNET_MessageHeader);
158 if (uri[uri_size - 1] != '\0')
161 return GNUNET_SYSERR;
168 * Core handler for p2p hostlist advertisements
171 * @param message advertisement message we got
172 * @return #GNUNET_OK on success
175 handle_advertisement (void *cls,
176 const struct GNUNET_MessageHeader *message)
178 const char *uri = (const char *) &message[1];
180 GNUNET_assert (NULL != client_adv_handler);
181 (void) (*client_adv_handler) (uri);
186 * Method called whenever a given peer connects. Wrapper to call both
187 * client's and server's functions
190 * @param peer peer identity this notification is about
191 * @param mq queue for sending messages to @a peer
195 connect_handler (void *cls,
196 const struct GNUNET_PeerIdentity *peer,
197 struct GNUNET_MQ_Handle *mq)
199 if (0 == memcmp (&me,
201 sizeof (struct GNUNET_PeerIdentity)))
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "A new peer connected, notifying client and server\n");
205 if (NULL != client_ch)
206 GNUNET_assert (NULL ==
211 if (NULL != server_ch)
212 GNUNET_assert (NULL ==
217 return (void *) peer;
222 * Method called whenever a given peer disconnects. Wrapper to call
223 * both client's and server's functions
226 * @param peer peer identity this notification is about
229 disconnect_handler (void *cls,
230 const struct GNUNET_PeerIdentity *peer,
233 if (0 == memcmp (&me,
235 sizeof (struct GNUNET_PeerIdentity)))
237 /* call hostlist client disconnect handler */
238 if (NULL != client_dh)
246 * Last task run during shutdown. Disconnects us from
247 * the other services.
252 cleaning_task (void *cls)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Hostlist daemon is shutting down\n");
258 GNUNET_CORE_disconnect (core);
263 GNUNET_HOSTLIST_client_stop ();
266 if (provide_hostlist)
268 GNUNET_HOSTLIST_server_stop ();
273 GNUNET_STATISTICS_destroy (stats,
281 * Main function that will be run.
284 * @param args remaining command-line arguments
285 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
286 * @param cfg configuration
292 const struct GNUNET_CONFIGURATION_Handle *cfg)
294 struct GNUNET_MQ_MessageHandler learn_handlers[] = {
295 GNUNET_MQ_hd_var_size (advertisement,
296 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT,
297 struct GNUNET_MessageHeader,
299 GNUNET_MQ_handler_end ()
301 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
302 GNUNET_MQ_handler_end ()
304 if ((! bootstrapping) && (! learning)
306 && (! provide_hostlist)
310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
314 stats = GNUNET_STATISTICS_create ("hostlist", cfg);
321 GNUNET_HOSTLIST_client_start (cfg,
328 GNUNET_CORE_connect (cfg,
333 learning ? learn_handlers : no_learn_handlers);
337 if (provide_hostlist)
338 GNUNET_HOSTLIST_server_start (cfg,
344 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 _("Failed to connect to `%s' service.\n"), "core");
351 GNUNET_SCHEDULER_shutdown ();
358 * The main function for the hostlist daemon.
360 * @param argc number of arguments from the command line
361 * @param argv command line arguments
362 * @return 0 ok, 1 on error
365 main (int argc, char *const *argv)
367 struct GNUNET_GETOPT_CommandLineOption options[] = {
369 GNUNET_GETOPT_option_flag ('a',
371 gettext_noop ("advertise our hostlist to other peers"),
374 GNUNET_GETOPT_option_flag ('b',
376 gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
378 GNUNET_GETOPT_option_flag ('e',
380 gettext_noop ("enable learning about hostlist servers from other peers"),
383 GNUNET_GETOPT_option_flag ('p',
385 gettext_noop ("provide a hostlist server"),
388 GNUNET_GETOPT_OPTION_END
393 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
396 GNUNET_log_setup ("hostlist", "WARNING", NULL);
399 GNUNET_PROGRAM_run (argc, argv,
401 _("GNUnet hostlist server and client"),
403 &run, NULL)) ? 0 : 1;
404 GNUNET_free ((void*) argv);
408 /* end of gnunet-daemon-hostlist.c */