cleanup
[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 struct GNUNET_PeerIdentity me;
129
130 static void
131 core_init (void *cls, struct GNUNET_CORE_Handle *server,
132            const struct GNUNET_PeerIdentity *my_identity)
133 {
134   me = *my_identity;
135 }
136
137 /**
138  * Core handler for p2p hostlist advertisements
139  */
140 static int
141 advertisement_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
142                        const struct GNUNET_MessageHeader *message,
143                        const struct GNUNET_TRANSPORT_ATS_Information *atsi)
144 {
145   GNUNET_assert (NULL != client_adv_handler);
146   return (*client_adv_handler) (cls, peer, message, atsi);
147 }
148
149
150 /**
151  * Method called whenever a given peer connects.  Wrapper to call both client's and server's functions
152  *
153  * @param cls closure
154  * @param peer peer identity this notification is about
155  * @param atsi performance data
156  */
157 static void
158 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
159                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
160 {
161   if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
162     return;
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "A new peer connected, notifying client and server\n");
165   if (NULL != client_ch)
166     (*client_ch) (cls, peer, atsi);
167 #if HAVE_MHD
168   if (NULL != server_ch)
169     (*server_ch) (cls, peer, atsi);
170 #endif
171 }
172
173 /**
174  * Method called whenever a given peer disconnects. Wrapper to call both client's and server's functions
175  *
176  * @param cls closure
177  * @param peer peer identity this notification is about
178  */
179 static void
180 disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
181 {
182   if (0 == memcmp (&me, peer, sizeof (struct GNUNET_PeerIdentity)))
183     return;
184   /* call hostlist client disconnect handler */
185   if (NULL != client_dh)
186     (*client_dh) (cls, peer);
187 #if HAVE_MHD
188   /* call hostlist server disconnect handler */
189   if (NULL != server_dh)
190     (*server_dh) (cls, peer);
191 #endif
192 }
193
194 /**
195  * Last task run during shutdown.  Disconnects us from
196  * the other services.
197  */
198 static void
199 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 {
201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist daemon is shutting down\n");
202   if (core != NULL)
203   {
204     GNUNET_CORE_disconnect (core);
205     core = NULL;
206   }
207   if (bootstrapping)
208   {
209     GNUNET_HOSTLIST_client_stop ();
210   }
211 #if HAVE_MHD
212   if (provide_hostlist)
213   {
214     GNUNET_HOSTLIST_server_stop ();
215   }
216 #endif
217   if (stats != NULL)
218   {
219     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
220     stats = NULL;
221   }
222 }
223
224
225 /**
226  * Main function that will be run.
227  *
228  * @param cls closure
229  * @param args remaining command-line arguments
230  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
231  * @param cfg configuration
232  */
233 static void
234 run (void *cls, char *const *args, const char *cfgfile,
235      const struct GNUNET_CONFIGURATION_Handle *cfg)
236 {
237   static const struct GNUNET_CORE_MessageHandler learn_handlers[] = {
238     {&advertisement_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
239     {NULL, 0, 0}
240   };
241   static const struct GNUNET_CORE_MessageHandler no_learn_handlers[] = {
242     {NULL, 0, 0}
243   };
244   if ((!bootstrapping) && (!learning)
245 #if HAVE_MHD
246       && (!provide_hostlist)
247 #endif
248       )
249   {
250     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
251                 _
252                 ("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
253     return;
254   }
255
256
257
258   stats = GNUNET_STATISTICS_create ("hostlist", cfg);
259
260   core =
261       GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_handler,
262                            &disconnect_handler, NULL, GNUNET_NO, NULL,
263                            GNUNET_NO,
264                            learning ? learn_handlers : no_learn_handlers);
265
266   if (bootstrapping)
267   {
268     GNUNET_HOSTLIST_client_start (cfg, stats, &client_ch, &client_dh,
269                                   &client_adv_handler, learning);
270   }
271
272 #if HAVE_MHD
273   if (provide_hostlist)
274   {
275     GNUNET_HOSTLIST_server_start (cfg, stats, core, &server_ch, &server_dh,
276                                   advertising);
277   }
278 #endif
279   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
280                                 NULL);
281
282   if (NULL == core)
283   {
284     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285                 _("Failed to connect to `%s' service.\n"), "core");
286     GNUNET_SCHEDULER_shutdown ();
287     return;
288   }
289 }
290
291
292 /**
293  * The main function for the hostlist daemon.
294  *
295  * @param argc number of arguments from the command line
296  * @param argv command line arguments
297  * @return 0 ok, 1 on error
298  */
299 int
300 main (int argc, char *const *argv)
301 {
302   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
303 #if HAVE_MHD
304     {'a', "advertise", NULL,
305      gettext_noop ("advertise our hostlist to other peers"),
306      GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising},
307 #endif
308     {'b', "bootstrap", NULL,
309      gettext_noop
310      ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
311      GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping},
312     {'e', "enable-learning", NULL,
313      gettext_noop ("enable learning about hostlist servers from other peers"),
314      GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
315 #if HAVE_MHD
316     {'p', "provide-hostlist", NULL,
317      gettext_noop ("provide a hostlist server"),
318      GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
319 #endif
320     GNUNET_GETOPT_OPTION_END
321   };
322
323   int ret;
324
325   GNUNET_log_setup ("hostlist", "WARNING", NULL);
326   ret =
327       (GNUNET_OK ==
328        GNUNET_PROGRAM_run (argc, argv, "hostlist",
329                            _("GNUnet hostlist server and client"), options,
330                            &run, NULL)) ? 0 : 1;
331
332   return ret;
333 }
334
335 /* end of gnunet-daemon-hostlist.c */