aboutsummaryrefslogtreecommitdiff
path: root/src/service/hostlist/gnunet-daemon-hostlist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/hostlist/gnunet-daemon-hostlist.c')
-rw-r--r--src/service/hostlist/gnunet-daemon-hostlist.c401
1 files changed, 401 insertions, 0 deletions
diff --git a/src/service/hostlist/gnunet-daemon-hostlist.c b/src/service/hostlist/gnunet-daemon-hostlist.c
new file mode 100644
index 000000000..54e070f89
--- /dev/null
+++ b/src/service/hostlist/gnunet-daemon-hostlist.c
@@ -0,0 +1,401 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2007, 2008, 2009, 2014 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file hostlist/gnunet-daemon-hostlist.c
23 * @brief code for bootstrapping via hostlist servers
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet-daemon-hostlist_client.h"
28#include "gnunet_core_service.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_protocols.h"
31#include "gnunet_statistics_service.h"
32
33#include "gnunet-daemon-hostlist_server.h"
34
35/**
36 * Set if we are allowed to advertise our hostlist to others.
37 */
38static int advertising;
39
40/**
41 * Set if the user wants us to run a hostlist server.
42 */
43static int provide_hostlist;
44
45/**
46 * Handle to hostlist server's connect handler
47 */
48static GNUNET_CORE_ConnectEventHandler server_ch;
49
50/**
51 * Set if we are allowed to learn about peers by accessing
52 * hostlist servers.
53 */
54static int bootstrapping;
55
56/**
57 * Set if the user allows us to learn about new hostlists
58 * from the network.
59 */
60static int learning;
61
62/**
63 * Statistics handle.
64 */
65static struct GNUNET_STATISTICS_Handle *stats;
66
67/**
68 * Handle to the core service (NULL until we've connected to it).
69 */
70static struct GNUNET_CORE_Handle *core;
71
72/**
73 * Handle to the hostlist client's advertisement handler
74 */
75static GNUNET_HOSTLIST_UriHandler client_adv_handler;
76
77/**
78 * Handle to hostlist client's connect handler
79 */
80static GNUNET_CORE_ConnectEventHandler client_ch;
81
82/**
83 * Handle to hostlist client's disconnect handler
84 */
85static GNUNET_CORE_DisconnectEventHandler client_dh;
86
87GNUNET_NETWORK_STRUCT_BEGIN
88
89/**
90 * A HOSTLIST_ADV message is used to exchange information about
91 * hostlist advertisements. This struct is always
92 * followed by the actual url under which the hostlist can be obtained:
93 *
94 * 1) transport-name (0-terminated)
95 * 2) address-length (uint32_t, network byte order; possibly
96 * unaligned!)
97 * 3) address expiration (GNUNET_TIME_AbsoluteNBO); possibly
98 * unaligned!)
99 * 4) address (address-length bytes; possibly unaligned!)
100 */
101struct GNUNET_HOSTLIST_ADV_Message
102{
103 /**
104 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
105 */
106 struct GNUNET_MessageHeader header;
107
108 /**
109 * Always zero (for alignment).
110 */
111 uint32_t reserved GNUNET_PACKED;
112};
113GNUNET_NETWORK_STRUCT_END
114
115
116/**
117 * Our own peer identity.
118 */
119static struct GNUNET_PeerIdentity me;
120
121
122/**
123 * Callback invoked once our connection to CORE service is up.
124 *
125 * @param cls NULL
126 * @param my_identity our peer's identity
127 */
128static void
129core_init (void *cls,
130 const struct GNUNET_PeerIdentity *my_identity)
131{
132 me = *my_identity;
133}
134
135
136/**
137 * Core handler for p2p hostlist advertisements
138 *
139 * @param cls closure
140 * @param message advertisement message we got
141 * @return #GNUNET_OK if message is well-formed
142 */
143static int
144check_advertisement (void *cls,
145 const struct GNUNET_MessageHeader *message)
146{
147 size_t size;
148 size_t uri_size;
149 const char *uri;
150
151 size = ntohs (message->size);
152 if (size <= sizeof(struct GNUNET_MessageHeader))
153 {
154 GNUNET_break_op (0);
155 return GNUNET_SYSERR;
156 }
157 uri = (const char *) &message[1];
158 uri_size = size - sizeof(struct GNUNET_MessageHeader);
159 if (uri[uri_size - 1] != '\0')
160 {
161 GNUNET_break_op (0);
162 return GNUNET_SYSERR;
163 }
164 return GNUNET_OK;
165}
166
167
168/**
169 * Core handler for p2p hostlist advertisements
170 *
171 * @param cls closure
172 * @param message advertisement message we got
173 * @return #GNUNET_OK on success
174 */
175static void
176handle_advertisement (void *cls,
177 const struct GNUNET_MessageHeader *message)
178{
179 const char *uri = (const char *) &message[1];
180
181 GNUNET_assert (NULL != client_adv_handler);
182 (void) (*client_adv_handler)(uri);
183}
184
185
186/**
187 * Method called whenever a given peer connects. Wrapper to call both
188 * client's and server's functions
189 *
190 * @param cls closure
191 * @param peer peer identity this notification is about
192 * @param mq queue for sending messages to @a peer
193 * @return peer
194 */
195static void *
196connect_handler (void *cls,
197 const struct GNUNET_PeerIdentity *peer,
198 struct GNUNET_MQ_Handle *mq)
199{
200 if (0 == GNUNET_memcmp (&me,
201 peer))
202 return NULL;
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "A new peer connected, notifying client and server\n");
205 if (NULL != client_ch)
206 GNUNET_assert (NULL ==
207 (*client_ch)(cls,
208 peer,
209 mq));
210 if (NULL != server_ch)
211 GNUNET_assert (NULL ==
212 (*server_ch)(cls,
213 peer,
214 mq));
215 return (void *) peer;
216}
217
218
219/**
220 * Method called whenever a given peer disconnects. Wrapper to call
221 * both client's and server's functions
222 *
223 * @param cls closure
224 * @param peer peer identity this notification is about
225 */
226static void
227disconnect_handler (void *cls,
228 const struct GNUNET_PeerIdentity *peer,
229 void *internal_cls)
230{
231 if (0 == GNUNET_memcmp (&me,
232 peer))
233 return;
234 /* call hostlist client disconnect handler */
235 if (NULL != client_dh)
236 (*client_dh)(cls,
237 peer,
238 NULL);
239}
240
241
242/**
243 * Last task run during shutdown. Disconnects us from
244 * the other services.
245 *
246 * @param cls NULL
247 */
248static void
249cleaning_task (void *cls)
250{
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Hostlist daemon is shutting down\n");
253 if (NULL != core)
254 {
255 GNUNET_CORE_disconnect (core);
256 core = NULL;
257 }
258 if (bootstrapping)
259 {
260 GNUNET_HOSTLIST_client_stop ();
261 }
262 if (provide_hostlist)
263 {
264 GNUNET_HOSTLIST_server_stop ();
265 }
266 if (NULL != stats)
267 {
268 GNUNET_STATISTICS_destroy (stats,
269 GNUNET_NO);
270 stats = NULL;
271 }
272}
273
274
275/**
276 * Main function that will be run.
277 *
278 * @param cls closure
279 * @param args remaining command-line arguments
280 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
281 * @param cfg configuration
282 */
283static void
284run (void *cls,
285 char *const *args,
286 const char *cfgfile,
287 const struct GNUNET_CONFIGURATION_Handle *cfg)
288{
289 struct GNUNET_MQ_MessageHandler learn_handlers[] = {
290 GNUNET_MQ_hd_var_size (advertisement,
291 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT,
292 struct GNUNET_MessageHeader,
293 NULL),
294 GNUNET_MQ_handler_end ()
295 };
296 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
297 GNUNET_MQ_handler_end ()
298 };
299
300 if ((! bootstrapping) && (! learning)
301 && (! provide_hostlist)
302 )
303 {
304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 _ (
306 "None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
307 return;
308 }
309 stats = GNUNET_STATISTICS_create ("hostlist", cfg);
310 if (NULL == stats)
311 {
312 GNUNET_break (0);
313 return;
314 }
315 if (bootstrapping)
316 GNUNET_HOSTLIST_client_start (cfg,
317 stats,
318 &client_ch,
319 &client_dh,
320 &client_adv_handler,
321 learning);
322 core =
323 GNUNET_CORE_connect (cfg,
324 NULL,
325 &core_init,
326 &connect_handler,
327 &disconnect_handler,
328 learning ? learn_handlers : no_learn_handlers);
329
330
331 if (provide_hostlist)
332 GNUNET_HOSTLIST_server_start (cfg,
333 stats,
334 core,
335 &server_ch,
336 advertising);
337 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
338 NULL);
339
340 if (NULL == core)
341 {
342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
343 _ ("Failed to connect to `%s' service.\n"), "core");
344 GNUNET_SCHEDULER_shutdown ();
345 return;
346 }
347}
348
349
350/**
351 * The main function for the hostlist daemon.
352 *
353 * @param argc number of arguments from the command line
354 * @param argv command line arguments
355 * @return 0 ok, 1 on error
356 */
357int
358main (int argc, char *const *argv)
359{
360 struct GNUNET_GETOPT_CommandLineOption options[] = {
361 GNUNET_GETOPT_option_flag ('a',
362 "advertise",
363 gettext_noop (
364 "advertise our hostlist to other peers"),
365 &advertising),
366 GNUNET_GETOPT_option_flag ('b',
367 "bootstrap",
368 gettext_noop (
369 "bootstrap using hostlists (it is highly recommended that you always use this option)"),
370 &bootstrapping),
371 GNUNET_GETOPT_option_flag ('e',
372 "enable-learning",
373 gettext_noop (
374 "enable learning about hostlist servers from other peers"),
375 &learning),
376 GNUNET_GETOPT_option_flag ('p',
377 "provide-hostlist",
378 gettext_noop ("provide a hostlist server"),
379 &provide_hostlist),
380 GNUNET_GETOPT_OPTION_END
381 };
382
383 int ret;
384
385 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
386 return 2;
387
388 GNUNET_log_setup ("hostlist", "WARNING", NULL);
389 ret =
390 (GNUNET_OK ==
391 GNUNET_PROGRAM_run (argc, argv,
392 "hostlist",
393 _ ("GNUnet hostlist server and client"),
394 options,
395 &run, NULL)) ? 0 : 1;
396 GNUNET_free_nz ((void *) argv);
397 return ret;
398}
399
400
401/* end of gnunet-daemon-hostlist.c */