GNUnet  0.10.x
Data Structures | Functions | Variables
gnunet-daemon-hostlist.c File Reference

code for bootstrapping via hostlist servers More...

#include "platform.h"
#include "gnunet-daemon-hostlist_client.h"
#include "gnunet_core_service.h"
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_statistics_service.h"
Include dependency graph for gnunet-daemon-hostlist.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_HOSTLIST_ADV_Message
 A HOSTLIST_ADV message is used to exchange information about hostlist advertisements. More...
 

Functions

static void core_init (void *cls, const struct GNUNET_PeerIdentity *my_identity)
 Callback invoked once our connection to CORE service is up. More...
 
static int check_advertisement (void *cls, const struct GNUNET_MessageHeader *message)
 Core handler for p2p hostlist advertisements. More...
 
static void handle_advertisement (void *cls, const struct GNUNET_MessageHeader *message)
 Core handler for p2p hostlist advertisements. More...
 
static void * connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
 Method called whenever a given peer connects. More...
 
static void disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer, void *internal_cls)
 Method called whenever a given peer disconnects. More...
 
static void cleaning_task (void *cls)
 Last task run during shutdown. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Main function that will be run. More...
 
int main (int argc, char *const *argv)
 The main function for the hostlist daemon. More...
 

Variables

static int bootstrapping
 Set if we are allowed to learn about peers by accessing hostlist servers. More...
 
static int learning
 Set if the user allows us to learn about new hostlists from the network. More...
 
static struct GNUNET_STATISTICS_Handlestats
 Statistics handle. More...
 
static struct GNUNET_CORE_Handlecore
 Handle to the core service (NULL until we've connected to it). More...
 
static GNUNET_HOSTLIST_UriHandler client_adv_handler
 Handle to the hostlist client's advertisement handler. More...
 
static GNUNET_CORE_ConnectEventHandler client_ch
 Handle to hostlist client's connect handler. More...
 
static GNUNET_CORE_DisconnectEventHandler client_dh
 Handle to hostlist client's disconnect handler. More...
 
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
 Our own peer identity. More...
 

Detailed Description

code for bootstrapping via hostlist servers

Author
Christian Grothoff

Definition in file gnunet-daemon-hostlist.c.

Function Documentation

◆ core_init()

static void core_init ( void *  cls,
const struct GNUNET_PeerIdentity my_identity 
)
static

Callback invoked once our connection to CORE service is up.

Parameters
clsNULL
my_identityour peer's identity

Definition at line 133 of file gnunet-daemon-hostlist.c.

References me, and my_identity.

Referenced by run().

135 {
136  me = *my_identity;
137 }
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
Here is the caller graph for this function:

◆ check_advertisement()

static int check_advertisement ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Core handler for p2p hostlist advertisements.

Parameters
clsclosure
messageadvertisement message we got
Returns
GNUNET_OK if message is well-formed

Definition at line 148 of file gnunet-daemon-hostlist.c.

References GNUNET_break_op, GNUNET_OK, GNUNET_SYSERR, size, GNUNET_MessageHeader::size, and uri.

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 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
Header for all communications.

◆ handle_advertisement()

static void handle_advertisement ( void *  cls,
const struct GNUNET_MessageHeader message 
)
static

Core handler for p2p hostlist advertisements.

Parameters
clsclosure
messageadvertisement message we got
Returns
GNUNET_OK on success

Definition at line 180 of file gnunet-daemon-hostlist.c.

References client_adv_handler, GNUNET_assert, and uri.

182 {
183  const char *uri = (const char *) &message[1];
184 
186  (void) (*client_adv_handler) (uri);
187 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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 GNUNET_HOSTLIST_UriHandler client_adv_handler
Handle to the hostlist client&#39;s advertisement handler.

◆ connect_handler()

static void* connect_handler ( void *  cls,
const struct GNUNET_PeerIdentity peer,
struct GNUNET_MQ_Handle mq 
)
static

Method called whenever a given peer connects.

Wrapper to call both client's and server's functions

Parameters
clsclosure
peerpeer identity this notification is about
mqqueue for sending messages to peer
Returns
peer

Definition at line 200 of file gnunet-daemon-hostlist.c.

References client_ch, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_memcmp, me, and peer.

Referenced by run().

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 }
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_log(kind,...)
static GNUNET_CORE_ConnectEventHandler client_ch
Handle to hostlist client&#39;s connect handler.
Here is the caller graph for this function:

◆ disconnect_handler()

static void disconnect_handler ( void *  cls,
const struct GNUNET_PeerIdentity peer,
void *  internal_cls 
)
static

Method called whenever a given peer disconnects.

Wrapper to call both client's and server's functions

Parameters
clsclosure
peerpeer identity this notification is about

Definition at line 233 of file gnunet-daemon-hostlist.c.

References client_dh, GNUNET_memcmp, me, and peer.

Referenced by run().

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 }
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
static GNUNET_CORE_DisconnectEventHandler client_dh
Handle to hostlist client&#39;s disconnect handler.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
Here is the caller graph for this function:

◆ cleaning_task()

static void cleaning_task ( void *  cls)
static

Last task run during shutdown.

Disconnects us from the other services.

Parameters
clsNULL

Definition at line 255 of file gnunet-daemon-hostlist.c.

References bootstrapping, GNUNET_CORE_disconnect(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_HOSTLIST_client_stop(), GNUNET_HOSTLIST_server_stop(), GNUNET_log, GNUNET_NO, and GNUNET_STATISTICS_destroy().

Referenced by run().

256 {
258  "Hostlist daemon is shutting down\n");
259  if (NULL != core)
260  {
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 }
void GNUNET_HOSTLIST_client_stop()
Stop downloading hostlists from hostlist servers as necessary.
#define GNUNET_NO
Definition: gnunet_common.h:81
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle)
Disconnect from the core service.
Definition: core_api.c:732
static struct GNUNET_CORE_Handle * core
Handle to the core service (NULL until we&#39;ve connected to it).
static int bootstrapping
Set if we are allowed to learn about peers by accessing hostlist servers.
void GNUNET_HOSTLIST_server_stop()
Stop server offering our hostlist.
#define GNUNET_log(kind,...)
static struct GNUNET_STATISTICS_Handle * stats
Statistics handle.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Main function that will be run.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgconfiguration

Definition at line 292 of file gnunet-daemon-hostlist.c.

References _, advertising, bootstrapping, cleaning_task(), client_adv_handler, client_ch, client_dh, connect_handler(), core_init(), disconnect_handler(), GNUNET_break, GNUNET_CORE_connect(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_HOSTLIST_client_start(), GNUNET_HOSTLIST_server_start(), GNUNET_log, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_STATISTICS_create(), and learning.

Referenced by main().

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  if ((! bootstrapping) && (! learning)
308 #if HAVE_MHD
309  && (! provide_hostlist)
310 #endif
311  )
312  {
314  _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
315  return;
316  }
317  stats = GNUNET_STATISTICS_create ("hostlist", cfg);
318  if (NULL == stats)
319  {
320  GNUNET_break (0);
321  return;
322  }
323  if (bootstrapping)
325  stats,
326  &client_ch,
327  &client_dh,
329  learning);
330  core =
331  GNUNET_CORE_connect (cfg,
332  NULL,
333  &core_init,
336  learning ? learn_handlers : no_learn_handlers);
337 
338 
339 #if HAVE_MHD
340  if (provide_hostlist)
342  stats,
343  core,
344  &server_ch,
345  advertising);
346 #endif
348  NULL);
349 
350  if (NULL == core)
351  {
353  _("Failed to connect to `%s' service.\n"), "core");
355  return;
356  }
357 }
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:1293
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
static void disconnect_handler(void *cls, const struct GNUNET_PeerIdentity *peer, void *internal_cls)
Method called whenever a given peer disconnects.
static void * connect_handler(void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq)
Method called whenever a given peer connects.
static void core_init(void *cls, const struct GNUNET_PeerIdentity *my_identity)
Callback invoked once our connection to CORE service is up.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static void cleaning_task(void *cls)
Last task run during shutdown.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static int advertising
Set if we are allowed to advertise our hostlist to others.
static GNUNET_CORE_DisconnectEventHandler client_dh
Handle to hostlist client&#39;s disconnect handler.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
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_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:694
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.
static struct GNUNET_CORE_Handle * core
Handle to the core service (NULL until we&#39;ve connected to it).
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_log(kind,...)
#define GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT
Further X-VINE DHT messages continued from 880.
Header for all communications.
static struct GNUNET_STATISTICS_Handle * stats
Statistics handle.
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function for the hostlist daemon.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 368 of file gnunet-daemon-hostlist.c.

References _, advertising, bootstrapping, gettext_noop, GNUNET_free, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_log_setup(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), learning, ret, and run().

369 {
371 #if HAVE_MHD
373  "advertise",
374  gettext_noop ("advertise our hostlist to other peers"),
375  &advertising),
376 #endif
378  "bootstrap",
379  gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
380  &bootstrapping),
382  "enable-learning",
383  gettext_noop ("enable learning about hostlist servers from other peers"),
384  &learning),
385 #if HAVE_MHD
387  "provide-hostlist",
388  gettext_noop ("provide a hostlist server"),
389  &provide_hostlist),
390 #endif
392  };
393 
394  int ret;
395 
396  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
397  return 2;
398 
399  GNUNET_log_setup ("hostlist", "WARNING", NULL);
400  ret =
401  (GNUNET_OK ==
402  GNUNET_PROGRAM_run (argc, argv,
403  "hostlist",
404  _("GNUnet hostlist server and client"),
405  options,
406  &run, NULL)) ? 0 : 1;
407  GNUNET_free ((void*) argv);
408  return ret;
409 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1521
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static int advertising
Set if we are allowed to advertise our hostlist to others.
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.
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.
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:361
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
#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.
Here is the call graph for this function:

Variable Documentation

◆ bootstrapping

int bootstrapping
static

Set if we are allowed to learn about peers by accessing hostlist servers.

Definition at line 58 of file gnunet-daemon-hostlist.c.

Referenced by cleaning_task(), main(), and run().

◆ learning

int learning
static

Set if the user allows us to learn about new hostlists from the network.

Definition at line 64 of file gnunet-daemon-hostlist.c.

Referenced by main(), and run().

◆ stats

struct GNUNET_STATISTICS_Handle* stats
static

Statistics handle.

Definition at line 69 of file gnunet-daemon-hostlist.c.

◆ core

struct GNUNET_CORE_Handle* core
static

Handle to the core service (NULL until we've connected to it).

Definition at line 74 of file gnunet-daemon-hostlist.c.

◆ client_adv_handler

GNUNET_HOSTLIST_UriHandler client_adv_handler
static

Handle to the hostlist client's advertisement handler.

Definition at line 79 of file gnunet-daemon-hostlist.c.

Referenced by handle_advertisement(), and run().

◆ client_ch

Handle to hostlist client's connect handler.

Definition at line 84 of file gnunet-daemon-hostlist.c.

Referenced by connect_handler(), and run().

◆ client_dh

Handle to hostlist client's disconnect handler.

Definition at line 89 of file gnunet-daemon-hostlist.c.

Referenced by disconnect_handler(), and run().

◆ me