133308edf90af1b5a13eb66eaf4daa8764690100
[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 /**
47  * Set if we are allowed to advertise our hostlist to others.
48  */
49 static int advertising;
50
51 /**
52  * Set if we are allowed to learn about peers by accessing
53  * hostlist servers.
54  */
55 static int bootstrapping;
56
57 /**
58  * Set if the user allows us to learn about new hostlists
59  * from the network.
60  */
61 static int learning;
62
63 /**
64  * Set if the user wants us to run a hostlist server.
65  */
66 static int provide_hostlist;
67
68 /**
69  * Statistics handle.
70  */
71 static struct GNUNET_STATISTICS_Handle *stats;
72
73 /**
74  * Handle to the core service (NULL until we've connected to it).
75  */
76 struct GNUNET_CORE_Handle *core;
77
78 /**
79  * gnunet-daemon-hostlist command line options.
80  */
81 static struct GNUNET_GETOPT_CommandLineOption options[] = {
82   { 'a', "advertise", NULL, 
83     gettext_noop ("advertise our hostlist to other peers"),
84     GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
85   { 'b', "bootstrap", NULL, 
86     gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
87     GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
88   { 'e', "enable-learning", NULL,
89     gettext_noop ("enable learning about hostlist servers from other peers"),
90     GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
91   { 'p', "provide-hostlist", NULL, 
92     gettext_noop ("provide a hostlist server"),
93     GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
94   GNUNET_GETOPT_OPTION_END
95 };
96
97
98 static void
99 core_init (void *cls,
100            struct GNUNET_CORE_Handle * server,
101            const struct GNUNET_PeerIdentity *
102            my_identity,
103            const struct
104            GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
105            publicKey)
106 {
107   if (advertising && (NULL != server))
108     {    
109       /* FIXME: provide "server" to 'hostlist' module */
110     }
111 }
112
113
114 /**
115  * Last task run during shutdown.  Disconnects us from
116  * the other services.
117  */
118 static void
119 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122               "Hostlist daemon is shutting down.\n");
123   if (bootstrapping)
124     {
125       GNUNET_HOSTLIST_client_stop ();
126     }
127   if (provide_hostlist)
128     {      
129       GNUNET_HOSTLIST_server_stop ();
130     }
131   if (core != NULL)
132     {
133       GNUNET_CORE_disconnect (core);
134       core = NULL;
135     }
136   if (stats != NULL)
137     {
138       GNUNET_STATISTICS_destroy (stats,
139                                  GNUNET_NO);
140       stats = NULL;
141     }
142 }
143
144
145 /**
146  * Main function that will be run.
147  *
148  * @param cls closure
149  * @param sched the scheduler to use
150  * @param args remaining command-line arguments
151  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
152  * @param cfg configuration
153  */
154 static void 
155 run (void *cls,
156      struct GNUNET_SCHEDULER_Handle * sched,
157      char *const *args,
158      const char *cfgfile,
159      const struct GNUNET_CONFIGURATION_Handle * cfg)
160 {
161   GNUNET_CORE_ConnectEventHandler ch = NULL;
162   GNUNET_CORE_DisconnectEventHandler dh = NULL;
163   struct GNUNET_CORE_MessageHandler handlers[] = 
164     {
165       { NULL, 0, 0 }
166     };
167
168   if ( (! bootstrapping) &&
169        (! learning) &&
170        (! provide_hostlist) )
171     {
172       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173                   _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
174       return;
175     }
176   stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
177   if (bootstrapping)
178     {
179       GNUNET_HOSTLIST_client_start (cfg, sched, stats,
180                                     &ch, &dh);
181     }
182   if (provide_hostlist)
183     {      
184       GNUNET_HOSTLIST_server_start (cfg, sched, stats);
185     }
186   if (learning)
187     {
188       /* FIXME (register handler with core for hostlist ads) */
189     }
190   core = GNUNET_CORE_connect (sched, cfg,
191                               GNUNET_TIME_UNIT_FOREVER_REL,
192                               NULL,
193                               &core_init,
194                               NULL, ch, dh,
195                               NULL, GNUNET_NO,
196                               NULL, GNUNET_NO,
197                               handlers);
198   GNUNET_SCHEDULER_add_delayed (sched,
199                                 GNUNET_TIME_UNIT_FOREVER_REL,
200                                 &cleaning_task, NULL);
201   if (NULL == core)
202     {
203       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204                   _("Failed to connect to `%s' service.\n"),
205                   "core");
206       GNUNET_SCHEDULER_shutdown (sched);
207       return;     
208     }
209 }
210
211
212 /**
213  * The main function for the hostlist daemon.
214  *
215  * @param argc number of arguments from the command line
216  * @param argv command line arguments
217  * @return 0 ok, 1 on error
218  */
219 int
220 main (int argc, char *const *argv)
221 {
222   int ret;
223
224   ret = (GNUNET_OK ==
225          GNUNET_PROGRAM_run (argc,
226                              argv,
227                              "hostlist", 
228                              _("GNUnet hostlist server and client"),
229                              options,
230                              &run, NULL)) ? 0 : 1;
231   return ret;
232 }
233
234 /* end of gnunet-daemon-hostlist.c */