GNUnet  0.11.x
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 
33 #if HAVE_MHD
34 
36 
40 static int advertising;
41 
45 static int provide_hostlist;
46 
50 static GNUNET_CORE_ConnectEventHandler server_ch;
51 
52 #endif
53 
58 static int bootstrapping;
59 
64 static int learning;
65 
70 
74 static struct GNUNET_CORE_Handle *core;
75 
80 
85 
90 
92 
106 {
111 
116 };
118 
119 
123 static struct GNUNET_PeerIdentity me;
124 
125 
132 static void
133 core_init (void *cls,
134  const struct GNUNET_PeerIdentity *my_identity)
135 {
136  me = *my_identity;
137 }
138 
139 
147 static int
149  const struct GNUNET_MessageHeader *message)
150 {
151  size_t size;
152  size_t uri_size;
153  const char *uri;
154 
155  size = ntohs (message->size);
156  if (size <= sizeof(struct GNUNET_MessageHeader))
157  {
158  GNUNET_break_op (0);
159  return GNUNET_SYSERR;
160  }
161  uri = (const char *) &message[1];
162  uri_size = size - sizeof(struct GNUNET_MessageHeader);
163  if (uri[uri_size - 1] != '\0')
164  {
165  GNUNET_break_op (0);
166  return GNUNET_SYSERR;
167  }
168  return GNUNET_OK;
169 }
170 
171 
179 static void
181  const struct GNUNET_MessageHeader *message)
182 {
183  const char *uri = (const char *) &message[1];
184 
186  (void) (*client_adv_handler)(uri);
187 }
188 
189 
199 static void *
200 connect_handler (void *cls,
201  const struct GNUNET_PeerIdentity *peer,
202  struct GNUNET_MQ_Handle *mq)
203 {
204  if (0 == GNUNET_memcmp (&me,
205  peer))
206  return NULL;
208  "A new peer connected, notifying client and server\n");
209  if (NULL != client_ch)
210  GNUNET_assert (NULL ==
211  (*client_ch)(cls,
212  peer,
213  mq));
214 #if HAVE_MHD
215  if (NULL != server_ch)
216  GNUNET_assert (NULL ==
217  (*server_ch)(cls,
218  peer,
219  mq));
220 #endif
221  return (void *) peer;
222 }
223 
224 
232 static void
234  const struct GNUNET_PeerIdentity *peer,
235  void *internal_cls)
236 {
237  if (0 == GNUNET_memcmp (&me,
238  peer))
239  return;
240  /* call hostlist client disconnect handler */
241  if (NULL != client_dh)
242  (*client_dh)(cls,
243  peer,
244  NULL);
245 }
246 
247 
254 static void
255 cleaning_task (void *cls)
256 {
258  "Hostlist daemon is shutting down\n");
259  if (NULL != core)
260  {
261  GNUNET_CORE_disconnect (core);
262  core = NULL;
263  }
264  if (bootstrapping)
265  {
267  }
268 #if HAVE_MHD
269  if (provide_hostlist)
270  {
272  }
273 #endif
274  if (NULL != stats)
275  {
277  GNUNET_NO);
278  stats = NULL;
279  }
280 }
281 
282 
291 static void
292 run (void *cls,
293  char *const *args,
294  const char *cfgfile,
295  const struct GNUNET_CONFIGURATION_Handle *cfg)
296 {
297  struct GNUNET_MQ_MessageHandler learn_handlers[] = {
298  GNUNET_MQ_hd_var_size (advertisement,
300  struct GNUNET_MessageHeader,
301  NULL),
303  };
304  struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
306  };
307 
308  if ((! bootstrapping) && (! learning)
309 #if HAVE_MHD
310  && (! provide_hostlist)
311 #endif
312  )
313  {
315  _ (
316  "None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
317  return;
318  }
319  stats = GNUNET_STATISTICS_create ("hostlist", cfg);
320  if (NULL == stats)
321  {
322  GNUNET_break (0);
323  return;
324  }
325  if (bootstrapping)
327  stats,
328  &client_ch,
329  &client_dh,
331  learning);
332  core =
333  GNUNET_CORE_connect (cfg,
334  NULL,
335  &core_init,
338  learning ? learn_handlers : no_learn_handlers);
339 
340 
341 #if HAVE_MHD
342  if (provide_hostlist)
344  stats,
345  core,
346  &server_ch,
347  advertising);
348 #endif
350  NULL);
351 
352  if (NULL == core)
353  {
355  _ ("Failed to connect to `%s' service.\n"), "core");
357  return;
358  }
359 }
360 
361 
369 int
370 main (int argc, char *const *argv)
371 {
372  struct GNUNET_GETOPT_CommandLineOption options[] = {
373 #if HAVE_MHD
375  "advertise",
376  gettext_noop (
377  "advertise our hostlist to other peers"),
378  &advertising),
379 #endif
381  "bootstrap",
382  gettext_noop (
383  "bootstrap using hostlists (it is highly recommended that you always use this option)"),
384  &bootstrapping),
386  "enable-learning",
387  gettext_noop (
388  "enable learning about hostlist servers from other peers"),
389  &learning),
390 #if HAVE_MHD
392  "provide-hostlist",
393  gettext_noop ("provide a hostlist server"),
394  &provide_hostlist),
395 #endif
397  };
398 
399  int ret;
400 
401  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
402  return 2;
403 
404  GNUNET_log_setup ("hostlist", "WARNING", NULL);
405  ret =
406  (GNUNET_OK ==
407  GNUNET_PROGRAM_run (argc, argv,
408  "hostlist",
409  _ ("GNUnet hostlist server and client"),
410  options,
411  &run, NULL)) ? 0 : 1;
412  GNUNET_free ((void*) argv);
413  return ret;
414 }
415 
416 
417 /* end of gnunet-daemon-hostlist.c */
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
void GNUNET_HOSTLIST_client_stop()
Stop downloading hostlists from hostlist servers as necessary.
Context for the core service connection.
Definition: core_api.c:77
static void handle_advertisement(void *cls, const struct GNUNET_MessageHeader *message)
Core handler for p2p hostlist advertisements.
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.
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1438
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void(* GNUNET_CORE_DisconnectEventHandler)(void *cls, const struct GNUNET_PeerIdentity *peer, void *peer_cls)
Method called whenever a peer disconnects.
static void disconnect_handler(void *cls, const struct GNUNET_PeerIdentity *peer, void *internal_cls)
Method called whenever a given peer disconnects.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
static void * connect_handler(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
Method called whenever a given peer connects.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static void core_init(void *cls, const struct GNUNET_PeerIdentity *my_identity)
Callback invoked once our connection to CORE service is up.
Definition of a command line option.
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
Handle for the service.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32.
static void cleaning_task(void *cls)
Last task run during shutdown.
void *(* GNUNET_CORE_ConnectEventHandler)(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
Method called whenever a given peer connects.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int main(int argc, char *const *argv)
The main function for the hostlist daemon.
A HOSTLIST_ADV message is used to exchange information about hostlist advertisements.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static int advertising
Set if we are allowed to advertise our hostlist to others.
void(* GNUNET_HOSTLIST_UriHandler)(const char *uri)
Function that handles an advertised URI.
static GNUNET_CORE_DisconnectEventHandler client_dh
Handle to hostlist client&#39;s disconnect handler.
static int check_advertisement(void *cls, const struct GNUNET_MessageHeader *message)
Core handler for p2p hostlist advertisements.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
static int learning
Set if the user allows us to learn about new hostlists from the network.
Message handler for a specific message type.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
uint32_t reserved
Always zero (for alignment).
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
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:692
void GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle)
Disconnect from the core service.
Definition: core_api.c:730
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.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
Handle to a message queue.
Definition: mq.c:85
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won&#39;t work on W32;.
static struct GNUNET_CORE_Handle * core
Handle to the core service (NULL until we&#39;ve connected to it).
The identity of the host (wraps the signing key of the peer).
static int bootstrapping
Set if we are allowed to learn about peers by accessing hostlist servers.
static GNUNET_HOSTLIST_UriHandler client_adv_handler
Handle to the hostlist client&#39;s advertisement handler.
#define GNUNET_PACKED
gcc-ism to get packed structs.
configuration data
Definition: configuration.c:85
void GNUNET_HOSTLIST_server_stop()
Stop server offering our hostlist.
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.
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_log(kind,...)
#define GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT
Further X-VINE DHT messages continued from 880.
int GNUNET_PROGRAM_run(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, parse options).
Definition: program.c:367
Header for all communications.
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
static struct GNUNET_STATISTICS_Handle * stats
Statistics handle.
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
static GNUNET_CORE_ConnectEventHandler client_ch
Handle to hostlist client&#39;s connect handler.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.