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