create hostlist client if we have no MHD
[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 2, 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 = NULL;
57
58 /**
59  * Handle to hostlist server's disconnect handler
60  */
61 static GNUNET_CORE_DisconnectEventHandler server_dh = NULL;
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 = NULL;
91
92 /**
93  * Handle to hostlist client's connect handler
94  */
95 static GNUNET_CORE_ConnectEventHandler client_ch = NULL;
96
97 /**
98  * Handle to hostlist client's disconnect handler
99  */
100 static GNUNET_CORE_DisconnectEventHandler client_dh = NULL;
101
102 /**
103  * gnunet-daemon-hostlist command line options.
104  */
105 static struct GNUNET_GETOPT_CommandLineOption options[] = {
106 #if HAVE_MHD
107   { 'a', "advertise", NULL, 
108     gettext_noop ("advertise our hostlist to other peers"),
109     GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
110 #endif
111   { 'b', "bootstrap", NULL, 
112     gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
113     GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
114   { 'e', "enable-learning", NULL,
115     gettext_noop ("enable learning about hostlist servers from other peers"),
116     GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
117 #if HAVE_MHD
118   { 'p', "provide-hostlist", NULL, 
119     gettext_noop ("provide a hostlist server"),
120     GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
121 #endif
122   GNUNET_GETOPT_OPTION_END
123 };
124
125 /**
126  * A HOSTLIST_ADV message is used to exchange information about
127  * hostlist advertisements.  This struct is always
128  * followed by the actual url under which the hostlist can be obtained:
129  *
130  * 1) transport-name (0-terminated)
131  * 2) address-length (uint32_t, network byte order; possibly
132  *    unaligned!)
133  * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
134  *    unaligned!)
135  * 4) address (address-length bytes; possibly unaligned!)
136  */
137 struct GNUNET_HOSTLIST_ADV_Message
138 {
139   /**
140    * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
141    */
142   struct GNUNET_MessageHeader header;
143
144   /**
145    * Always zero (for alignment).
146    */
147   uint32_t reserved GNUNET_PACKED;
148 };
149
150
151 static void
152 core_init (void *cls,
153            struct GNUNET_CORE_Handle * server,
154            const struct GNUNET_PeerIdentity *
155            my_identity,
156            const struct
157            GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
158            publicKey)
159 {
160 #if HAVE_MHD
161   if (advertising && (NULL != server))
162     {    
163       /* FIXME: provide "server" to 'hostlist' module */
164     }
165 #endif
166 }
167
168 /**
169  * Core handler for p2p hostlist advertisements
170  */
171 static int advertisement_handler (void *cls,
172                              const struct GNUNET_PeerIdentity * peer,
173                              const struct GNUNET_MessageHeader * message,
174                              struct GNUNET_TIME_Relative latency,
175                              uint32_t distance)
176 {
177   GNUNET_assert (NULL != client_adv_handler);
178   return (*client_adv_handler) (cls, peer, message, latency, distance);
179 }
180
181
182 /**
183  * Method called whenever a given peer connects.  Wrapper to call both client's and server's functions
184  *
185  * @param cls closure
186  * @param peer peer identity this notification is about
187  * @param latency reported latency of the connection with 'other'
188  * @param distance reported distance (DV) to 'other'
189  */
190 static void
191 connect_handler (void *cls,
192                  const struct
193                  GNUNET_PeerIdentity * peer,
194                  struct GNUNET_TIME_Relative latency,
195                  uint32_t distance)
196 {
197   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198               "A new peer connected, notifying client and server\n");
199   if ( NULL != client_ch)
200     (*client_ch) (cls, peer, latency, distance);
201 #if HAVE_MHD
202   if ( NULL != server_ch)
203     (*server_ch) (cls, peer, latency, distance);
204 #endif
205 }
206
207 /**
208  * Method called whenever a given peer disconnects. Wrapper to call both client's and server's functions
209  *
210  * @param cls closure
211  * @param peer peer identity this notification is about
212  */
213 static void
214 disconnect_handler (void *cls,
215                     const struct
216                     GNUNET_PeerIdentity * peer)
217 {
218
219   /* call hostlist client disconnect handler*/
220   if ( NULL != client_dh)
221     (*client_dh) (cls, peer);
222 #if HAVE_MHD
223   /* call hostlist server disconnect handler*/
224   if ( NULL != server_dh)
225     (*server_dh) (cls, peer);
226 #endif
227 }
228
229 /**
230  * Last task run during shutdown.  Disconnects us from
231  * the other services.
232  */
233 static void
234 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 {
236   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237               "Hostlist daemon is shutting down\n");
238   if (bootstrapping)
239     {
240       GNUNET_HOSTLIST_client_stop ();
241     }
242 #if HAVE_MHD
243   if (provide_hostlist)
244     {      
245       GNUNET_HOSTLIST_server_stop ();
246     }
247 #endif
248   if (core != NULL)
249     {
250       GNUNET_CORE_disconnect (core);
251       core = NULL;
252     }
253   if (stats != NULL)
254     {
255       GNUNET_STATISTICS_destroy (stats,
256                                  GNUNET_NO);
257       stats = NULL;
258     }
259 }
260
261 /**
262  * List of handlers if we are learning.
263  */
264 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
265   { &advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
266   { NULL, 0, 0 }
267 };
268
269
270 /**
271  * List of handlers if we are not learning.
272  */
273 static struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
274     { NULL, 0, 0 }
275 };
276
277
278 /**
279  * Main function that will be run.
280  *
281  * @param cls closure
282  * @param sched the scheduler to use
283  * @param args remaining command-line arguments
284  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
285  * @param cfg configuration
286  */
287 static void 
288 run (void *cls,
289      struct GNUNET_SCHEDULER_Handle * sched,
290      char *const *args,
291      const char *cfgfile,
292      const struct GNUNET_CONFIGURATION_Handle * cfg)
293 {
294   if ( (! bootstrapping) &&
295        (! learning) 
296 #if HAVE_MHD
297        && (! provide_hostlist) 
298 #endif
299        )
300     {
301       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
302                   _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
303       return;
304     }
305   stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
306
307   core = GNUNET_CORE_connect (sched, cfg,
308                               GNUNET_TIME_UNIT_FOREVER_REL,
309                               NULL,
310                               &core_init,
311                               &connect_handler, &disconnect_handler,
312                               NULL, GNUNET_NO,
313                               NULL, GNUNET_NO,
314                               learning? learn_handlers : no_learn_handlers);
315
316   if (bootstrapping)
317     {
318       GNUNET_HOSTLIST_client_start (cfg, sched, stats,
319                                     &client_ch, &client_dh, &client_adv_handler, learning);
320     }
321 #if HAVE_MHD
322   if (provide_hostlist)
323     {      
324       GNUNET_HOSTLIST_server_start (cfg, sched, stats, core, &server_ch, &server_dh, advertising );
325     }
326 #endif
327   GNUNET_SCHEDULER_add_delayed (sched,
328                                 GNUNET_TIME_UNIT_FOREVER_REL,
329                                 &cleaning_task, NULL);
330   if (NULL == core)
331     {
332       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333                   _("Failed to connect to `%s' service.\n"),
334                   "core");
335       GNUNET_SCHEDULER_shutdown (sched);
336       return;     
337     }
338 }
339
340
341 /**
342  * The main function for the hostlist daemon.
343  *
344  * @param argc number of arguments from the command line
345  * @param argv command line arguments
346  * @return 0 ok, 1 on error
347  */
348 int
349 main (int argc, char *const *argv)
350 {
351   int ret;
352   GNUNET_log_setup ("hostlist","DEBUG",NULL);
353
354   ret = (GNUNET_OK ==
355          GNUNET_PROGRAM_run (argc,
356                              argv,
357                              "hostlist", 
358                              _("GNUnet hostlist server and client"),
359                              options,
360                              &run, NULL)) ? 0 : 1;
361
362   return ret;
363 }
364
365 /* end of gnunet-daemon-hostlist.c */