GNUnet 0.22.2
gnunet-daemon-hostlist.c
Go to the documentation of this file.
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
26#include "platform.h"
28#include "gnunet_core_service.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_protocols.h"
32
34
38static int advertising;
39
44
49
54static int bootstrapping;
55
60static int learning;
61
66
70static struct GNUNET_CORE_Handle *core;
71
76
81
86
88
102{
107
112};
114
115
120
121
128static void
129core_init (void *cls,
130 const struct GNUNET_PeerIdentity *my_identity)
131{
132 me = *my_identity;
133}
134
135
143static int
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
175static void
177 const struct GNUNET_MessageHeader *message)
178{
179 const char *uri = (const char *) &message[1];
180
182 (void) (*client_adv_handler)(uri);
183}
184
185
195static void *
197 const struct GNUNET_PeerIdentity *peer,
198 struct GNUNET_MQ_Handle *mq)
199{
200 if (0 == GNUNET_memcmp (&me,
201 peer))
202 return NULL;
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
226static void
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
248static void
249cleaning_task (void *cls)
250{
252 "Hostlist daemon is shutting down\n");
253 if (NULL != core)
254 {
256 core = NULL;
257 }
258 if (bootstrapping)
259 {
261 }
263 {
265 }
266 if (NULL != stats)
267 {
269 GNUNET_NO);
270 stats = NULL;
271 }
272}
273
274
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,
293 NULL),
295 };
296 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
298 };
299
300 if ((! bootstrapping) && (! learning)
301 && (! provide_hostlist)
302 )
303 {
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)
317 stats,
318 &client_ch,
319 &client_dh,
321 learning);
322 core =
324 NULL,
325 &core_init,
328 learning ? learn_handlers : no_learn_handlers);
329
330
333 stats,
334 core,
335 &server_ch,
338 NULL);
339
340 if (NULL == core)
341 {
343 _ ("Failed to connect to `%s' service.\n"), "core");
345 return;
346 }
347}
348
349
357int
358main (int argc, char *const *argv)
359{
362 "advertise",
364 "advertise our hostlist to other peers"),
365 &advertising),
367 "bootstrap",
369 "bootstrap using hostlists (it is highly recommended that you always use this option)"),
372 "enable-learning",
374 "enable learning about hostlist servers from other peers"),
375 &learning),
377 "provide-hostlist",
378 gettext_noop ("provide a hostlist server"),
381 };
382
383 int ret;
384
385 GNUNET_log_setup ("hostlist", "WARNING", NULL);
386 ret =
387 (GNUNET_OK ==
389 argc, argv,
390 "hostlist",
391 _ ("GNUnet hostlist server and client"),
392 options,
393 &run, NULL)) ? 0 : 1;
394 return ret;
395}
396
397
398/* end of gnunet-daemon-hostlist.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define gettext_noop(String)
Definition: gettext.h:74
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
struct GNUNET_PeerIdentity my_identity
Our peer identity.
static void cleaning_task(void *cls)
Last task run during shutdown.
static struct GNUNET_CORE_Handle * core
Handle to the core service (NULL until we've connected to it).
static int advertising
Set if we are allowed to advertise our hostlist to others.
static void * connect_handler(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
Method called whenever a given peer connects.
static GNUNET_CORE_ConnectEventHandler server_ch
Handle to hostlist server's connect handler.
static struct GNUNET_STATISTICS_Handle * stats
Statistics handle.
static int check_advertisement(void *cls, const struct GNUNET_MessageHeader *message)
Core handler for p2p hostlist advertisements.
static int learning
Set if the user allows us to learn about new hostlists from the network.
static int bootstrapping
Set if we are allowed to learn about peers by accessing hostlist servers.
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
static GNUNET_HOSTLIST_UriHandler client_adv_handler
Handle to the hostlist client's advertisement handler.
static void disconnect_handler(void *cls, const struct GNUNET_PeerIdentity *peer, void *internal_cls)
Method called whenever a given peer disconnects.
static void handle_advertisement(void *cls, const struct GNUNET_MessageHeader *message)
Core handler for p2p hostlist advertisements.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.
int main(int argc, char *const *argv)
The main function for the hostlist daemon.
static int provide_hostlist
Set if the user wants us to run a hostlist server.
static void core_init(void *cls, const struct GNUNET_PeerIdentity *my_identity)
Callback invoked once our connection to CORE service is up.
static GNUNET_CORE_ConnectEventHandler client_ch
Handle to hostlist client's connect handler.
static GNUNET_CORE_DisconnectEventHandler client_dh
Handle to hostlist client's disconnect handler.
int GNUNET_HOSTLIST_client_start(const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_STATISTICS_Handle *st, GNUNET_CORE_ConnectEventHandler *ch, GNUNET_CORE_DisconnectEventHandler *dh, GNUNET_HOSTLIST_UriHandler *msgh, int learn)
Start downloading hostlists from hostlist servers as necessary.
void GNUNET_HOSTLIST_client_stop()
Stop downloading hostlists from hostlist servers as necessary.
void(* GNUNET_HOSTLIST_UriHandler)(const char *uri)
Function that handles an advertised URI.
int GNUNET_HOSTLIST_server_start(const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_STATISTICS_Handle *st, struct GNUNET_CORE_Handle *co, GNUNET_CORE_ConnectEventHandler *server_ch, int advertise)
Start server offering our hostlist.
void GNUNET_HOSTLIST_server_stop()
Stop server offering our hostlist.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
Core service; the main API for encrypted P2P communications.
Constants for network protocols.
API to create, modify and access statistics.
void(* GNUNET_CORE_DisconnectEventHandler)(void *cls, const struct GNUNET_PeerIdentity *peer, void *peer_cls)
Method called whenever a peer disconnects.
struct GNUNET_CORE_Handle * GNUNET_CORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, GNUNET_CORE_StartupCallback init, GNUNET_CORE_ConnectEventHandler connects, GNUNET_CORE_DisconnectEventHandler disconnects, const struct GNUNET_MQ_MessageHandler *handlers)
Connect to the core service.
Definition: core_api.c:691
void GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle)
Disconnect from the core service.
Definition: core_api.c:729
void *(* GNUNET_CORE_ConnectEventHandler)(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
Method called whenever a given peer connects.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
#define GNUNET_log(kind,...)
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_PACKED
gcc-ism to get packed structs.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
enum GNUNET_GenericReturnValue GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:407
#define GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT
Hostlist advertisement message.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1339
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
Context for the core service connection.
Definition: core_api.c:78
Definition of a command line option.
A HOSTLIST_ADV message is used to exchange information about hostlist advertisements.
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
uint32_t reserved
Always zero (for alignment).
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Header for all communications.
The identity of the host (wraps the signing key of the peer).
Handle for the service.