2c4aca6d0d1d07a5c066e022159b508f2e38290e
[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   if (stats != NULL)
122     {
123       GNUNET_STATISTICS_destroy (stats,
124                                  GNUNET_NO);
125       stats = NULL;
126     }
127   GNUNET_CORE_disconnect (core);
128 }
129
130
131 /**
132  * Main function that will be run.
133  *
134  * @param cls closure
135  * @param sched the scheduler to use
136  * @param args remaining command-line arguments
137  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
138  * @param cfg configuration
139  */
140 static void 
141 run (void *cls,
142      struct GNUNET_SCHEDULER_Handle * sched,
143      char *const *args,
144      const char *cfgfile,
145      const struct GNUNET_CONFIGURATION_Handle * cfg)
146 {
147   GNUNET_CORE_ClientEventHandler ch = NULL;
148   GNUNET_CORE_ClientEventHandler dh = NULL;
149   struct GNUNET_CORE_MessageHandler handlers[] = 
150     {
151       { NULL, 0, 0 }
152     };
153
154   if ( (! bootstrapping) &&
155        (! learning) &&
156        (! provide_hostlist) )
157     {
158       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
159                   _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
160       return;
161     }
162   stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
163   if (learning)
164     {
165       /* FIXME (register handler with core for hostlist ads) */
166     }
167   if (bootstrapping)
168     {
169       GNUNET_HOSTLIST_client_start (cfg, sched, stats,
170                                     &ch, &dh);
171     }
172   if (provide_hostlist)
173     {      
174       GNUNET_HOSTLIST_server_start (cfg, sched, stats);
175     }
176   core = GNUNET_CORE_connect (sched, cfg,
177                               GNUNET_TIME_UNIT_FOREVER_REL,
178                               NULL,
179                               &core_init,
180                               ch, dh,
181                               NULL,
182                               NULL, GNUNET_NO,
183                               NULL, GNUNET_NO,
184                               handlers);
185   GNUNET_SCHEDULER_add_delayed (sched,
186                                 GNUNET_TIME_UNIT_FOREVER_REL,
187                                 &cleaning_task, NULL);
188   if (NULL == core)
189     {
190       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191                   _("Failed to connect to `%s' service.\n"),
192                   "core");
193       GNUNET_SCHEDULER_shutdown (sched);
194       return;     
195     }
196 }
197
198
199 /**
200  * The main function for the hostlist daemon.
201  *
202  * @param argc number of arguments from the command line
203  * @param argv command line arguments
204  * @return 0 ok, 1 on error
205  */
206 int
207 main (int argc, char *const *argv)
208 {
209   int ret;
210
211   ret = (GNUNET_OK ==
212          GNUNET_PROGRAM_run (argc,
213                              argv,
214                              "hostlist", 
215                              _("GNUnet hostlist server and client"),
216                              options,
217                              &run, NULL)) ? 0 : 1;
218   return ret;
219 }
220
221 /* end of gnunet-daemon-hostlist.c */