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