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