no-verb
[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  * gnunet-daemon-hostlist command line options.
75  */
76 static struct GNUNET_GETOPT_CommandLineOption options[] = {
77   { 'a', "advertise", NULL, 
78     gettext_noop ("advertise our hostlist to other peers"),
79     GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
80   { 'b', "bootstrap", NULL, 
81     gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
82     GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
83   { 'e', "enable-learning", NULL,
84     gettext_noop ("enable learning about hostlist servers from other peers"),
85     GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
86   { 'p', "provide-hostlist", NULL, 
87     gettext_noop ("provide a hostlist server"),
88     GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
89   GNUNET_GETOPT_OPTION_END
90 };
91
92
93 static void
94 core_init (void *cls,
95            struct GNUNET_CORE_Handle * server,
96            const struct GNUNET_PeerIdentity *
97            my_identity,
98            const struct
99            GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
100            publicKey)
101 {
102   if (advertising && (NULL != server))
103     {    
104       /* FIXME: provide "server" to 'hostlist' module */
105     }
106 }
107
108
109 /**
110  * Last task run during shutdown.  Disconnects us from
111  * the other services.
112  */
113 static void
114 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   if (stats != NULL)
117     {
118       GNUNET_STATISTICS_destroy (stats);
119       stats = NULL;
120     }
121 }
122
123
124 /**
125  * Main function that will be run.
126  *
127  * @param cls closure
128  * @param sched the scheduler to use
129  * @param args remaining command-line arguments
130  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
131  * @param cfg configuration
132  */
133 static void 
134 run (void *cls,
135      struct GNUNET_SCHEDULER_Handle * sched,
136      char *const *args,
137      const char *cfgfile,
138      const struct GNUNET_CONFIGURATION_Handle * cfg)
139 {
140   GNUNET_CORE_ClientEventHandler ch = NULL;
141   GNUNET_CORE_ClientEventHandler dh = NULL;
142   struct GNUNET_CORE_MessageHandler handlers[] = 
143     {
144       { NULL, 0, 0 }
145     };
146
147   if ( (! bootstrapping) &&
148        (! learning) &&
149        (! provide_hostlist) )
150     {
151       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
152                   _("None of the functions for the hostlist daemon were enabled.  I have no reason to run!\n"));
153       return;
154     }
155   stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
156   if (learning)
157     {
158       /* FIXME (register handler with core for hostlist ads) */
159     }
160   if (bootstrapping)
161     {
162       GNUNET_HOSTLIST_client_start (cfg, sched, stats,
163                                     &ch, &dh);
164     }
165   if (provide_hostlist)
166     {      
167       GNUNET_HOSTLIST_server_start (cfg, sched, stats);
168     }
169   GNUNET_CORE_connect (sched, cfg,
170                        GNUNET_TIME_UNIT_FOREVER_REL,
171                        NULL,
172                        &core_init,
173                        ch, dh,
174                        NULL,
175                        NULL, GNUNET_NO,
176                        NULL, GNUNET_NO,
177                        handlers);
178   GNUNET_SCHEDULER_add_delayed (sched,
179                                 GNUNET_YES,
180                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
181                                 GNUNET_SCHEDULER_NO_TASK,
182                                 GNUNET_TIME_UNIT_FOREVER_REL,
183                                 &cleaning_task, NULL);
184 }
185
186
187 /**
188  * The main function for the hostlist daemon.
189  *
190  * @param argc number of arguments from the command line
191  * @param argv command line arguments
192  * @return 0 ok, 1 on error
193  */
194 int
195 main (int argc, char *const *argv)
196 {
197   int ret;
198
199   ret = (GNUNET_OK ==
200          GNUNET_PROGRAM_run (argc,
201                              argv,
202                              "hostlist", 
203                              _("GNUnet hostlist server and client"),
204                              options,
205                              &run, NULL)) ? 0 : 1;
206   return ret;
207 }
208
209 /* end of gnunet-daemon-hostlist.c */