fix #3275 with solution from https://gnunet.org/bugs/view.php?id=3275#c8029
[oweals/gnunet.git] / src / hostlist / gnunet-daemon-hostlist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file hostlist/gnunet-daemon-hostlist.c
23  * @brief code for bootstrapping via hostlist servers
24  * @author Christian Grothoff
25  */
26
27 #include <stdlib.h>
28 #include "platform.h"
29 #include "hostlist-client.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_statistics_service.h"
34
35 #if HAVE_MHD
36
37 #include "hostlist-server.h"
38
39 /**
40  * Set if we are allowed to advertise our hostlist to others.
41  */
42 static int advertising;
43
44 /**
45  * Set if the user wants us to run a hostlist server.
46  */
47 static int provide_hostlist;
48
49 /**
50  * Handle to hostlist server's connect handler
51  */
52 static GNUNET_CORE_ConnectEventHandler server_ch;
53
54 /**
55  * Handle to hostlist server's disconnect handler
56  */
57 static GNUNET_CORE_DisconnectEventHandler server_dh;
58
59 #endif
60
61 /**
62  * Set if we are allowed to learn about peers by accessing
63  * hostlist servers.
64  */
65 static int bootstrapping;
66
67 /**
68  * Set if the user allows us to learn about new hostlists
69  * from the network.
70  */
71 static int learning;
72
73 /**
74  * Statistics handle.
75  */
76 static struct GNUNET_STATISTICS_Handle *stats;
77
78 /**
79  * Handle to the core service (NULL until we've connected to it).
80  */
81 static struct GNUNET_CORE_Handle *core;
82
83 /**
84  * Handle to the hostlist client's advertisement handler
85  */
86 static GNUNET_CORE_MessageCallback client_adv_handler;
87
88 /**
89  * Handle to hostlist client's connect handler
90  */
91 static GNUNET_CORE_ConnectEventHandler client_ch;
92
93 /**
94  * Handle to hostlist client's disconnect handler
95  */
96 static GNUNET_CORE_DisconnectEventHandler client_dh;
97
98 GNUNET_NETWORK_STRUCT_BEGIN
99
100 /**
101  * A HOSTLIST_ADV message is used to exchange information about
102  * hostlist advertisements.  This struct is always
103  * followed by the actual url under which the hostlist can be obtained:
104  *
105  * 1) transport-name (0-terminated)
106  * 2) address-length (uint32_t, network byte order; possibly
107  *    unaligned!)
108  * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
109  *    unaligned!)
110  * 4) address (address-length bytes; possibly unaligned!)
111  */
112 struct GNUNET_HOSTLIST_ADV_Message
113 {
114   /**
115    * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
116    */
117   struct GNUNET_MessageHeader header;
118
119   /**
120    * Always zero (for alignment).
121    */
122   uint32_t reserved GNUNET_PACKED;
123 };
124 GNUNET_NETWORK_STRUCT_END
125
126 static struct GNUNET_PeerIdentity me;
127
128
129 static void
130 core_init (void *cls,
131            const struct GNUNET_PeerIdentity *my_identity)
132 {
133   me = *my_identity;
134 }
135
136
137 /**
138  * Core handler for p2p hostlist advertisements
139  *
140  * @param cls closure
141  * @param peer identity of the sender
142  * @param message advertisement message we got
143  * @return #GNUNET_OK on success
144  */
145 static int
146 advertisement_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
147                        const struct GNUNET_MessageHeader *message)
148 {
149   GNUNET_assert (NULL != client_adv_handler);
150   return (*client_adv_handler) (cls, peer, message);
151 }
152
153
154 /**
155  * Method called whenever a given peer connects.  Wrapper to call both
156  * client's and server's functions
157  *
158  * @param cls closure
159  * @param peer peer identity this notification is about
160  */
161 static void
162 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
163 {
164   if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
165     return;
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167               "A new peer connected, notifying client and server\n");
168   if (NULL != client_ch)
169     (*client_ch) (cls, peer);
170 #if HAVE_MHD
171   if (NULL != server_ch)
172     (*server_ch) (cls, peer);
173 #endif
174 }
175
176
177 /**
178  * Method called whenever a given peer disconnects. Wrapper to call
179  * both client's and server's functions
180  *
181  * @param cls closure
182  * @param peer peer identity this notification is about
183  */
184 static void
185 disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
186 {
187   if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
188     return;
189   /* call hostlist client disconnect handler */
190   if (NULL != client_dh)
191     (*client_dh) (cls, peer);
192 #if HAVE_MHD
193   /* call hostlist server disconnect handler */
194   if (NULL != server_dh)
195     (*server_dh) (cls, peer);
196 #endif
197 }
198
199
200 /**
201  * Last task run during shutdown.  Disconnects us from
202  * the other services.
203  */
204 static void
205 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
206 {
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Hostlist daemon is shutting down\n");
209   if (core != NULL)
210   {
211     GNUNET_CORE_disconnect (core);
212     core = NULL;
213   }
214   if (bootstrapping)
215   {
216     GNUNET_HOSTLIST_client_stop ();
217   }
218 #if HAVE_MHD
219   if (provide_hostlist)
220   {
221     GNUNET_HOSTLIST_server_stop ();
222   }
223 #endif
224   if (stats != NULL)
225   {
226     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
227     stats = NULL;
228   }
229 }
230
231
232 /**
233  * Main function that will be run.
234  *
235  * @param cls closure
236  * @param args remaining command-line arguments
237  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
238  * @param cfg configuration
239  */
240 static void
241 run (void *cls, char *const *args, const char *cfgfile,
242      const struct GNUNET_CONFIGURATION_Handle *cfg)
243 {
244   static const struct GNUNET_CORE_MessageHandler learn_handlers[] = {
245     {&advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
246     {NULL, 0, 0}
247   };
248   static const struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
249     {NULL, 0, 0}
250   };
251   if ((!bootstrapping) && (!learning)
252 #if HAVE_MHD
253       && (!provide_hostlist)
254 #endif
255       )
256   {
257     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
258                 _
259                 ("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
260     return;
261   }
262   stats = GNUNET_STATISTICS_create ("hostlist", cfg);
263   if (bootstrapping)
264     GNUNET_HOSTLIST_client_start (cfg, stats, &client_ch, &client_dh,
265                                   &client_adv_handler, learning);
266   core =
267     GNUNET_CORE_connect (cfg, NULL,
268                          &core_init,
269                          &connect_handler,
270                          &disconnect_handler, NULL,
271                          GNUNET_NO, NULL,
272                          GNUNET_NO,
273                          learning ? learn_handlers : no_learn_handlers);
274
275
276 #if HAVE_MHD
277   if (provide_hostlist)
278     GNUNET_HOSTLIST_server_start (cfg, stats, core, &server_ch, &server_dh,
279                                   advertising);
280 #endif
281   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
282                                 &cleaning_task,
283                                 NULL);
284
285   if (NULL == core)
286   {
287     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288                 _("Failed to connect to `%s' service.\n"), "core");
289     GNUNET_SCHEDULER_shutdown ();
290     return;
291   }
292 }
293
294
295 /**
296  * The main function for the hostlist daemon.
297  *
298  * @param argc number of arguments from the command line
299  * @param argv command line arguments
300  * @return 0 ok, 1 on error
301  */
302 int
303 main (int argc, char *const *argv)
304 {
305   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
306 #if HAVE_MHD
307     {'a', "advertise", NULL,
308      gettext_noop ("advertise our hostlist to other peers"),
309      GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising},
310 #endif
311     {'b', "bootstrap", NULL,
312      gettext_noop
313      ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
314      GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping},
315     {'e', "enable-learning", NULL,
316      gettext_noop ("enable learning about hostlist servers from other peers"),
317      GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
318 #if HAVE_MHD
319     {'p', "provide-hostlist", NULL,
320      gettext_noop ("provide a hostlist server"),
321      GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
322 #endif
323     GNUNET_GETOPT_OPTION_END
324   };
325
326   int ret;
327
328   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
329     return 2;
330
331   GNUNET_log_setup ("hostlist", "WARNING", NULL);
332   ret =
333       (GNUNET_OK ==
334        GNUNET_PROGRAM_run (argc, argv, "hostlist",
335                            _("GNUnet hostlist server and client"), options,
336                            &run, NULL)) ? 0 : 1;
337   GNUNET_free ((void*) argv);
338   return ret;
339 }
340
341 /* end of gnunet-daemon-hostlist.c */