9f3453890b9b6278329fb89c9b031e545c56962e
[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 struct GNUNET_CORE_Handle *core;
76
77 /**
78  * gnunet-daemon-hostlist command line options.
79  */
80 static struct GNUNET_GETOPT_CommandLineOption options[] = {
81   { 'a', "advertise", NULL, 
82     gettext_noop ("advertise our hostlist to other peers"),
83     GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
84   { 'b', "bootstrap", NULL, 
85     gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
86     GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
87   { 'e', "enable-learning", NULL,
88     gettext_noop ("enable learning about hostlist servers from other peers"),
89     GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
90   { 'p', "provide-hostlist", NULL, 
91     gettext_noop ("provide a hostlist server"),
92     GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
93   GNUNET_GETOPT_OPTION_END
94 };
95
96
97 static void
98 core_init (void *cls,
99            struct GNUNET_CORE_Handle * server,
100            const struct GNUNET_PeerIdentity *
101            my_identity,
102            const struct
103            GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
104            publicKey)
105 {
106   if (advertising && (NULL != server))
107     {    
108       /* FIXME: provide "server" to 'hostlist' module */
109     }
110 }
111
112 /**
113  * Core handler for p2p hostlist advertisements
114  */
115 static int handle_hostlist_advertisement (void *cls,
116                              const struct GNUNET_PeerIdentity * peer,
117                              const struct GNUNET_MessageHeader * message,
118                              struct GNUNET_TIME_Relative latency,
119                              uint32_t distance)
120 {
121 #if DEBUG_HOSTLIST_LEARNING
122       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123                   _("Recieved hostlist advertisement\n"));
124 #endif
125
126       return GNUNET_OK;
127 }
128
129 /**
130  * Last task run during shutdown.  Disconnects us from
131  * the other services.
132  */
133 static void
134 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137               "Hostlist daemon is shutting down\n");
138   if (bootstrapping)
139     {
140       GNUNET_HOSTLIST_client_stop ();
141     }
142   if (provide_hostlist)
143     {      
144       GNUNET_HOSTLIST_server_stop ();
145     }
146   if (core != NULL)
147     {
148       GNUNET_CORE_disconnect (core);
149       core = NULL;
150     }
151   if (stats != NULL)
152     {
153       GNUNET_STATISTICS_destroy (stats,
154                                  GNUNET_NO);
155       stats = NULL;
156     }
157 }
158
159 /**
160  * List of handlers for the messages understood by this
161  * service.
162  */
163 static struct GNUNET_CORE_MessageHandler handlers[] = {
164     { &handle_hostlist_advertisement, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
165     { NULL, 0, 0 }
166 };
167
168 /**
169  * Main function that will be run.
170  *
171  * @param cls closure
172  * @param sched the scheduler to use
173  * @param args remaining command-line arguments
174  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
175  * @param cfg configuration
176  */
177 static void 
178 run (void *cls,
179      struct GNUNET_SCHEDULER_Handle * sched,
180      char *const *args,
181      const char *cfgfile,
182      const struct GNUNET_CONFIGURATION_Handle * cfg)
183 {
184   GNUNET_CORE_ConnectEventHandler ch = NULL;
185   GNUNET_CORE_DisconnectEventHandler dh = NULL;
186
187
188
189   struct GNUNET_CORE_MessageHandler null_handler[] = {
190       { NULL, 0, 0 }
191   };
192
193   struct GNUNET_CORE_MessageHandler *used_handler = null_handler;
194
195   if ( (! bootstrapping) &&
196        (! learning) &&
197        (! provide_hostlist) )
198     {
199       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
200                   _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
201       return;
202     }
203   stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
204   if (bootstrapping)
205     {
206       GNUNET_HOSTLIST_client_start (cfg, sched, stats,
207                                     &ch, &dh);
208     }
209   if (provide_hostlist)
210     {      
211       GNUNET_HOSTLIST_server_start (cfg, sched, stats);
212     }
213   if (learning)
214     {
215       used_handler = handlers;
216     }
217
218   core = GNUNET_CORE_connect (sched, cfg,
219                             GNUNET_TIME_UNIT_FOREVER_REL,
220                             NULL,
221                             &core_init,
222                             NULL, ch, dh,
223                             NULL, GNUNET_NO,
224                             NULL, GNUNET_NO,
225                             used_handler);
226
227   GNUNET_SCHEDULER_add_delayed (sched,
228                                 GNUNET_TIME_UNIT_FOREVER_REL,
229                                 &cleaning_task, NULL);
230   if (NULL == core)
231     {
232       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233                   _("Failed to connect to `%s' service.\n"),
234                   "core");
235       GNUNET_SCHEDULER_shutdown (sched);
236       return;     
237     }
238 }
239
240
241 /**
242  * The main function for the hostlist daemon.
243  *
244  * @param argc number of arguments from the command line
245  * @param argv command line arguments
246  * @return 0 ok, 1 on error
247  */
248 int
249 main (int argc, char *const *argv)
250 {
251   int ret;
252   GNUNET_log_setup ("hostlist","DEBUG",NULL);
253
254   ret = (GNUNET_OK ==
255          GNUNET_PROGRAM_run (argc,
256                              argv,
257                              "hostlist", 
258                              _("GNUnet hostlist server and client"),
259                              options,
260                              &run, NULL)) ? 0 : 1;
261
262   return ret;
263 }
264
265 /* end of gnunet-daemon-hostlist.c */