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