Exemple #1
0
static void
setup_peer (struct PeerContext *p,
            const char *cfgname)
{
  char *binary;

  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  p->cfg = GNUNET_CONFIGURATION_create ();
  p->arm_proc =
    GNUNET_OS_start_process (GNUNET_YES,
			     GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
			     NULL, NULL, NULL,
			     binary,
			     "gnunet-service-arm",
			     "-c",
			     cfgname,
			     NULL);
  GNUNET_assert (GNUNET_OK ==
		 GNUNET_CONFIGURATION_load (p->cfg,
					    cfgname));
  p->ats = GNUNET_ATS_connectivity_init (p->cfg);
  GNUNET_assert (NULL != p->ats);
  p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
				       GNUNET_TRANSPORT_AC_ANY,
				       &process_hello,
				       p);
  GNUNET_free (binary);
}
static char *
generate_config (char *cfg_file, unsigned long long quota_in,
                 unsigned long long quota_out)
{
  char *networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
  char *in_name;
  char *out_name;
  char *fname = NULL;
  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
  int c;

  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_file));
  GNUNET_asprintf (&fname, "q_in_%llu_q_out_%llu_%s", quota_in, quota_out,
                   cfg_file);

  GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", fname);

  for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
  {
      GNUNET_asprintf (&in_name, "%s_QUOTA_IN", networks[c]);
      GNUNET_asprintf (&out_name, "%s_QUOTA_OUT", networks[c]);
      GNUNET_CONFIGURATION_set_value_number (cfg, "ats", in_name, quota_in);
      GNUNET_CONFIGURATION_set_value_number (cfg, "ats", out_name, quota_out);
      GNUNET_free (in_name);
      GNUNET_free (out_name);
  }
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write (cfg, fname));
  GNUNET_CONFIGURATION_destroy (cfg);
  return fname;
}
static void
setup_peer (struct PeerContext *p, const char *cfgname)
{
  p->cfg = GNUNET_CONFIGURATION_create ();
#if START_ARM
  p->arm_proc =
      GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
                               "gnunet-service-arm",
                               "-c", cfgname, NULL);
#endif
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
}
Exemple #4
0
/**
 * Setup a key file for a peer given the name of the
 * configuration file (!).  This function is used so that
 * at a later point code can be certain that reading a
 * key is fast (for example in time-dependent testcases).
 *
 * @param cfg_name name of the configuration file to use
 */
void
GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
{
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_CRYPTO_EddsaPrivateKey *priv;

  cfg = GNUNET_CONFIGURATION_create ();
  (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
  priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
  if (NULL != priv)
    GNUNET_free (priv);
  GNUNET_CONFIGURATION_destroy (cfg);
}
static void
setup_peer (struct PeerContext *p, const char *cfgname)
{
  char *binary;

  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  p->cfg = GNUNET_CONFIGURATION_create ();
  p->arm_proc =
    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
			     NULL, NULL,
			     binary,
			     "gnunet-service-arm",
			     "-c", cfgname, NULL);
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  GNUNET_free (binary);
}
static char *
generate_config (char *cfg_file, unsigned long long quota_in,
                 unsigned long long quota_out)
{
  char *fname = NULL;
  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();

  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_file));
  GNUNET_asprintf (&fname, "q_in_%llu_q_out_%llu_%s", quota_in, quota_out,
                   cfg_file);
  GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", fname);
  GNUNET_CONFIGURATION_set_value_number (cfg, "ats", "WAN_QUOTA_IN", quota_in);
  GNUNET_CONFIGURATION_set_value_number (cfg, "ats", "WAN_QUOTA_OUT",
                                         quota_out);
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write (cfg, fname));
  GNUNET_CONFIGURATION_destroy (cfg);
  return fname;
}
static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
    struct GNUNET_CONFIGURATION_Handle *testing_cfg;

    ok = 1;
    testing_cfg = GNUNET_CONFIGURATION_create ();
    GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (testing_cfg, cfgfile));
#if VERBOSE
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
    GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
                                           "use_progressbars", "YES");
#endif
    peers_left = NUM_PEERS;
    pg = GNUNET_TESTING_peergroup_start (testing_cfg, peers_left, TIMEOUT, NULL,
                                         &my_cb, NULL, NULL);
    GNUNET_assert (pg != NULL);
}
static void
setup_peer (struct PeerContext *p, const char *cfgname)
{
  p->cfg = GNUNET_CONFIGURATION_create ();
#if START_ARM
  p->arm_proc =
      GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
                               "gnunet-service-arm",
#if VERBOSE
                               "-L", "DEBUG",
#endif
                               "-c", cfgname, NULL);
#endif
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  p->th =
      GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
  GNUNET_assert (p->th != NULL);
  p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
}
static int check_blacklist_config (char *cfg_file,
    struct GNUNET_PeerIdentity *peer, struct GNUNET_PeerIdentity *bl_peer)
{
  struct GNUNET_CONFIGURATION_Handle *cfg;
  char *section;
  char *peer_str;
  cfg = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
    GNUNET_CONFIGURATION_destroy (cfg);
    return GNUNET_SYSERR;
  }

  peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
  GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);

  if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
        "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
        cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
    GNUNET_CONFIGURATION_destroy (cfg);
    GNUNET_free (section);
    GNUNET_free (peer_str);
    return GNUNET_SYSERR;
  }

  GNUNET_log(GNUNET_ERROR_TYPE_INFO,
      "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
      cfg_file, peer_str, GNUNET_i2s_full(bl_peer));

  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_free (section);
  GNUNET_free (peer_str);
  return GNUNET_OK;
}
static void
setup_learn_peer (struct PeerContext *p, const char *cfgname)
{
  char *filename;
  unsigned int result;
  char *binary;

  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  p->cfg = GNUNET_CONFIGURATION_create ();
  p->arm_proc =
    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
                             NULL, NULL, NULL,
                             binary,
                             "gnunet-service-arm",
                             "-c", cfgname, NULL);
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_string (p->cfg, "HOSTLIST", "HOSTLISTFILE",
                                             &filename))
  {
    if (GNUNET_YES == GNUNET_DISK_file_test (filename))
    {
      result = UNLINK (filename);
      if (result == 0)
        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                    _("Hostlist file `%s' was removed\n"), filename);
    }
    GNUNET_free (filename);
  }
  p->core =
      GNUNET_CORE_connect (p->cfg, NULL, NULL, NULL, NULL, NULL, GNUNET_NO,
                           NULL, GNUNET_NO, learn_handlers);
  GNUNET_assert (NULL != p->core);
  p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
  GNUNET_assert (NULL != p->stats);
  GNUNET_free (binary);
}
int
main (int argc, char *argv[])
{
  int failureCount = 0;
  char *c;

  GNUNET_log_setup ("test_configuration", "WARNING", NULL);
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (cfg != NULL);
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_parse (cfg, "test_configuration_data.conf"))
  {
    FPRINTF (stderr, "%s",  "Failed to parse configuration file\n");
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  failureCount += testConfig ();
  if (failureCount > 0)
    goto error;

  failureCount = testConfigFilenames ();
  if (failureCount > 0)
    goto error;

  if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, "/tmp/gnunet-test.conf"))
  {
    FPRINTF (stderr, "%s",  "Failed to write configuration file\n");
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_assert (0 == UNLINK ("/tmp/gnunet-test.conf"));

  cfg = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_load (cfg, "test_configuration_data.conf"))
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "WEAKRANDOM", &c))
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }
  if (0 != strcmp (c, "YES"))
  {
    GNUNET_break (0);
    GNUNET_free (c);
    GNUNET_CONFIGURATION_destroy (cfg);
    return 1;
  }

  GNUNET_free (c);
  GNUNET_CONFIGURATION_destroy (cfg);

  /* Testing configuration diffs */
  cfgDefault = GNUNET_CONFIGURATION_create ();
  if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfgDefault, NULL))
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfgDefault);
    return 1;
  }

  /* Nothing changed in the new configuration */
  failureCount += checkDiffs (cfgDefault, EDIT_NOTHING);

  /* Modify all entries of the last section */
  failureCount += checkDiffs (cfgDefault, EDIT_SECTION);

  /* Add a new section */
  failureCount += checkDiffs (cfgDefault, ADD_NEW_SECTION);

  /* Add a new entry to the last section */
  failureCount += checkDiffs (cfgDefault, ADD_NEW_ENTRY);

  /* Modify all entries in the configuration */
  failureCount += checkDiffs (cfgDefault, EDIT_ALL);

  GNUNET_CONFIGURATION_destroy (cfgDefault);

error:
  if (failureCount != 0)
  {
    FPRINTF (stderr, "Test failed: %u\n", failureCount);
    return 1;
  }
  return 0;
}
/**
 * Start a peer with the given configuration
 * @param tth the testing handle
 * @param cfgname configuration file
 * @param peer_id a unique number to identify the peer
 * @param rec receive callback
 * @param nc connect callback
 * @param nd disconnect callback
 * @param start_cb start callback
 * @param cb_cls closure for callback
 * @return the peer context
 */
struct PeerContext *
GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
                                     const char *cfgname, int peer_id,
                                     GNUNET_TRANSPORT_ReceiveCallback rec,
                                     GNUNET_TRANSPORT_NotifyConnect nc,
                                     GNUNET_TRANSPORT_NotifyDisconnect nd,
                                     GNUNET_TRANSPORT_TESTING_start_cb start_cb,
                                     void *cb_cls)
{
  char *emsg = NULL;
  struct GNUNET_PeerIdentity *dummy;

  GNUNET_assert (NULL != tth);
  GNUNET_assert (NULL != tth->tl_system);

  if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
                     "File not found: `%s' \n", cfgname);
    return NULL;
  }

  struct PeerContext *p = GNUNET_new (struct PeerContext);
  GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);

  /* Create configuration and call testing lib to modify it */
  p->cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));

  if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
                     "Testing library failed to create unique configuration based on `%s'\n",
                     cfgname);
    GNUNET_free (p);
    return NULL;
  }

  p->no = peer_id;
  /* Configure peer with configuration */
  p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, p->cfg, p->no, NULL, &emsg);
  if (NULL == p->peer)
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
                     "Testing library failed to create unique configuration based on `%s': `%s'\n",
                     cfgname, emsg);
    GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
    GNUNET_free_non_null (emsg);
    return NULL;
  }
  GNUNET_free_non_null (emsg);
  if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
                     "Testing library failed to create unique configuration based on `%s'\n",
                     cfgname);
    GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
    return NULL;
  }

  memset(&dummy, '\0', sizeof (dummy));
  GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
  if (0 == memcmp (&dummy, &p->id, sizeof (struct GNUNET_PeerIdentity)))
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
                     "Testing library failed to obtain peer identity for peer %u\n",
                     p->no);
    GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
    return NULL;
  }
  else
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
                     "Peer %u configured with identity `%s'\n",
                     p->no,
                     GNUNET_i2s_full (&p->id));
  }

  p->tth = tth;
  p->nc = nc;
  p->nd = nd;
  p->rec = rec;
  p->start_cb = start_cb;
  if (cb_cls != NULL)
    p->cb_cls = cb_cls;
  else
    p->cb_cls = p;

  p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
                                    &notify_receive,
                                    &notify_connect, &notify_disconnect);
  if (NULL == p->th)
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
                     "Failed to connect to transport service for peer  `%s': `%s'\n",
                     cfgname, emsg);
    GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
    return NULL;
  }

  p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
  GNUNET_assert (p->ghh != NULL);

  return p;
}
Exemple #13
0
/**
 * Run a standard GNUnet service startup sequence (initialize loggers
 * and configuration, parse options).
 *
 * @param argc number of command line arguments
 * @param argv command line arguments
 * @param service_name our service name
 * @param options service options
 * @param task main task of the service
 * @param task_cls closure for @a task
 * @return #GNUNET_SYSERR on error, #GNUNET_OK
 *         if we shutdown nicely
 */
int
GNUNET_SERVICE_run (int argc, char *const *argv,
                    const char *service_name,
                    enum GNUNET_SERVICE_Options options,
                    GNUNET_SERVICE_Main task,
                    void *task_cls)
{
#define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)

  int err;
  int ret;
  char *cfg_fn;
  char *opt_cfg_fn;
  char *loglev;
  char *logfile;
  int do_daemonize;
  unsigned int i;
  unsigned long long skew_offset;
  unsigned long long skew_variance;
  long long clock_offset;
  struct GNUNET_SERVICE_Context sctx;
  struct GNUNET_CONFIGURATION_Handle *cfg;
  const char *xdg;

  struct GNUNET_GETOPT_CommandLineOption service_options[] = {
    GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_fn),
    {'d', "daemonize", NULL,
     gettext_noop ("do daemonize (detach from terminal)"), 0,
     GNUNET_GETOPT_set_one, &do_daemonize},
    GNUNET_GETOPT_OPTION_HELP (NULL),
    GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
    GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
    GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
    GNUNET_GETOPT_OPTION_END
  };
  err = 1;
  do_daemonize = 0;
  logfile = NULL;
  loglev = NULL;
  opt_cfg_fn = NULL;
  xdg = getenv ("XDG_CONFIG_HOME");
  if (NULL != xdg)
    GNUNET_asprintf (&cfg_fn,
                     "%s%s%s",
                     xdg,
                     DIR_SEPARATOR_STR,
                     "gnunet.conf");
  else
    cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
  memset (&sctx, 0, sizeof (sctx));
  sctx.options = options;
  sctx.ready_confirm_fd = -1;
  sctx.ret = GNUNET_OK;
  sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
  sctx.task = task;
  sctx.task_cls = task_cls;
  sctx.service_name = service_name;
  sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();

  /* setup subsystems */
  ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
  if (GNUNET_SYSERR == ret)
    goto shutdown;
  if (GNUNET_NO == ret)
  {
    err = 0;
    goto shutdown;
  }
  if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
    HANDLE_ERROR;
  if (NULL == opt_cfg_fn)
    opt_cfg_fn = GNUNET_strdup (cfg_fn);
  if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_fn))
  {
    if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, opt_cfg_fn))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Malformed configuration file `%s', exit ...\n"),
                  opt_cfg_fn);
      goto shutdown;
    }
  }
  else
  {
    if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Malformed configuration, exit ...\n"));
      goto shutdown;
    }
    if (0 != strcmp (opt_cfg_fn, cfg_fn))
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Could not access configuration file `%s'\n"),
		  opt_cfg_fn);
  }
  if (GNUNET_OK != setup_service (&sctx))
    goto shutdown;
  if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
    HANDLE_ERROR;
  if (GNUNET_OK != set_user_id (&sctx))
    goto shutdown;
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Service `%s' runs with configuration from `%s'\n",
       service_name,
       opt_cfg_fn);
  if ((GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
                                              "SKEW_OFFSET", &skew_offset)) &&
      (GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
                                              "SKEW_VARIANCE", &skew_variance)))
  {
    clock_offset = skew_offset - skew_variance;
    GNUNET_TIME_set_offset (clock_offset);
    LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
  }
  /* actually run service */
  err = 0;
  GNUNET_SCHEDULER_run (&service_task, &sctx);
  /* shutdown */
  if ((1 == do_daemonize) && (NULL != sctx.server))
    pid_file_delete (&sctx);
  GNUNET_free_non_null (sctx.my_handlers);

shutdown:
  if (-1 != sctx.ready_confirm_fd)
  {
    if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
      LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
    GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
  }
#if HAVE_MALLINFO
  {
    char *counter;

    if ( (GNUNET_YES ==
	  GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
					   "GAUGER_HEAP")) &&
	 (GNUNET_OK ==
	  GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
						 "GAUGER_HEAP",
						 &counter)) )
    {
      struct mallinfo mi;

      mi = mallinfo ();
      GAUGER (service_name, counter, mi.usmblks, "blocks");
      GNUNET_free (counter);
    }
  }
#endif
  GNUNET_SPEEDUP_stop_ ();
  GNUNET_CONFIGURATION_destroy (cfg);
  i = 0;
  if (NULL != sctx.addrs)
    while (NULL != sctx.addrs[i])
      GNUNET_free (sctx.addrs[i++]);
  GNUNET_free_non_null (sctx.addrs);
  GNUNET_free_non_null (sctx.addrlens);
  GNUNET_free_non_null (logfile);
  GNUNET_free_non_null (loglev);
  GNUNET_free (cfg_fn);
  GNUNET_free_non_null (opt_cfg_fn);
  GNUNET_free_non_null (sctx.v4_denied);
  GNUNET_free_non_null (sctx.v6_denied);
  GNUNET_free_non_null (sctx.v4_allowed);
  GNUNET_free_non_null (sctx.v6_allowed);

  return err ? GNUNET_SYSERR : sctx.ret;
}
Exemple #14
0
/**
 * Run a standard GNUnet command startup sequence (initialize loggers
 * and configuration, parse options).
 *
 * @param argc number of command line arguments
 * @param argv command line arguments
 * @param binaryName our expected name
 * @param binaryHelp help text for the program
 * @param options command line options
 * @param task main function to run
 * @param task_cls closure for task
 * @param run_without_scheduler GNUNET_NO start the scheduler, GNUNET_YES do not
 *        start the scheduler just run the main task
 * @return GNUNET_SYSERR on error, GNUNET_OK on success
 */
int
GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
                    const char *binaryHelp,
                    const struct GNUNET_GETOPT_CommandLineOption *options,
                    GNUNET_PROGRAM_Main task, void *task_cls,
                    int run_without_scheduler)
{
  struct CommandContext cc;
  char *path;
  char *loglev;
  char *logfile;
  int ret;
  unsigned int cnt;
  unsigned long long skew_offset;
  unsigned long long skew_variance;
  long long clock_offset;
  struct GNUNET_CONFIGURATION_Handle *cfg;

  struct GNUNET_GETOPT_CommandLineOption defoptions[] = {
    GNUNET_GETOPT_OPTION_CFG_FILE (&cc.cfgfile),
    GNUNET_GETOPT_OPTION_HELP (binaryHelp),
    GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
    GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
    GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION)
  };
  struct GNUNET_GETOPT_CommandLineOption *allopts;
  const char *gargs;
  char *lpfx;
  char *spc;

  logfile = NULL;
  gargs = getenv ("GNUNET_ARGS");
  if (gargs != NULL)
  {
    char **gargv;
    unsigned int gargc;
    int i;
    char *tok;
    char *cargs;

    gargv = NULL;
    gargc = 0;
    for (i = 0; i < argc; i++)
      GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
    cargs = GNUNET_strdup (gargs);
    tok = strtok (cargs, " ");
    while (NULL != tok)
    {
      GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
      tok = strtok (NULL, " ");
    }
    GNUNET_free (cargs);
    GNUNET_array_append (gargv, gargc, NULL);
    argv = (char *const *) gargv;
    argc = gargc - 1;
  }
  memset (&cc, 0, sizeof (cc));
  loglev = NULL;
  cc.task = task;
  cc.task_cls = task_cls;
  cc.cfg = cfg = GNUNET_CONFIGURATION_create ();

  /* prepare */
#if ENABLE_NLS
  setlocale (LC_ALL, "");
  path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
  if (path != NULL)
  {
    BINDTEXTDOMAIN ("GNUnet", path);
    GNUNET_free (path);
  }
  textdomain ("GNUnet");
#endif
  cnt = 0;
  while (options[cnt].name != NULL)
    cnt++;
  allopts =
      GNUNET_malloc ((cnt +
                      1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
                     sizeof (defoptions));
  memcpy (allopts, defoptions, sizeof (defoptions));
  memcpy (&allopts
          [sizeof (defoptions) /
           sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
          (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
  cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
  qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption),
         &cmd_sorter);
  loglev = NULL;
  cc.cfgfile = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
  lpfx = GNUNET_strdup (binaryName);
  if (NULL != (spc = strstr (lpfx, " ")))
    *spc = '\0';
  if ((-1 ==
       (ret =
        GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv))) ||
      (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile)))
  {
    GNUNET_CONFIGURATION_destroy (cfg);
    GNUNET_free_non_null (cc.cfgfile);
    GNUNET_free_non_null (loglev);
    GNUNET_free_non_null (logfile);
    GNUNET_free (allopts);
    GNUNET_free (lpfx);
    return GNUNET_SYSERR;
  }
  (void) GNUNET_CONFIGURATION_load (cfg, cc.cfgfile);
  GNUNET_free (allopts);
  GNUNET_free (lpfx);
  if (GNUNET_OK ==
      GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset",
                                             &skew_offset) &&
      (GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing",
                                              "skew_variance", &skew_variance)))
  {
    clock_offset = skew_offset - skew_variance;
    GNUNET_TIME_set_offset (clock_offset);
  }
  /* run */
  cc.args = &argv[ret];
  if (GNUNET_NO == run_without_scheduler)
  {
          GNUNET_SCHEDULER_run (&program_main, &cc);
  }
  else
  {
          GNUNET_RESOLVER_connect (cc.cfg);
          cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg);
  }
  /* clean up */
  GNUNET_SPEEDUP_stop_ ();
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_free_non_null (cc.cfgfile);
  GNUNET_free_non_null (loglev);
  GNUNET_free_non_null (logfile);
  return GNUNET_OK;
}
Exemple #15
0
/**
 * Function called with the result from the autoconfiguration.
 *
 * @param cls closure
 * @param diff minimal suggested changes to the original configuration
 *             to make it work (as best as we can)
 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
 * @param type what the situation of the NAT
 */
static void
auto_config_cb (void *cls,
		const struct GNUNET_CONFIGURATION_Handle *diff,
		enum GNUNET_NAT_StatusCode result,
		enum GNUNET_NAT_Type type)
{
  const char *nat_type;
  char unknown_type[64];
  struct GNUNET_CONFIGURATION_Handle *new_cfg;

  ah = NULL;
  switch (type)
  {
  case GNUNET_NAT_TYPE_NO_NAT:
    nat_type = "NO NAT";
    break;
  case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
    nat_type = "NAT but we can traverse";
    break;
  case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
    nat_type = "NAT but STUN is able to identify the correct information";
    break;
  case GNUNET_NAT_TYPE_UPNP_NAT:
    nat_type = "NAT but UPNP opened the ports";
    break;
  default:
    SPRINTF (unknown_type,
	     "NAT unknown, type %u",
	     type);
    nat_type = unknown_type;
    break;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
	      "NAT status: %s/%s\n",
	      GNUNET_NAT_AUTO_status2string (result),
	      nat_type);

  /* Shortcut: if there are no changes suggested, bail out early. */
  if (GNUNET_NO ==
      GNUNET_CONFIGURATION_is_dirty (diff))
  {
    test_finished ();
    return;
  }

  /* Apply diff to original configuration and show changes
     to the user */
  new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;

  if (NULL != diff)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
		_("Suggested configuration changes:\n"));
    GNUNET_CONFIGURATION_iterate_section_values (diff,
						 "nat",
						 &auto_conf_iter,
						 new_cfg);
  }

  /* If desired, write configuration to file; we write only the
     changes to the defaults to keep things compact. */
  if ( (write_cfg) &&
       (NULL != diff) )
  {
    struct GNUNET_CONFIGURATION_Handle *def_cfg;

    GNUNET_CONFIGURATION_set_value_string (new_cfg,
					   "ARM",
					   "CONFIG",
					   NULL);
    def_cfg = GNUNET_CONFIGURATION_create ();
    GNUNET_break (GNUNET_OK ==
		  GNUNET_CONFIGURATION_load (def_cfg,
					     NULL));
    if (GNUNET_OK !=
	GNUNET_CONFIGURATION_write_diffs (def_cfg,
					  new_cfg,
					  cfg_file))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
		  _("Failed to write configuration to `%s'\n"),
		  cfg_file);
      global_ret = 1;
    }
    else
    {
      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
		  _("Wrote updated configuration to `%s'\n"),
		  cfg_file);
    }
    GNUNET_CONFIGURATION_destroy (def_cfg);
  }

  if (NULL != new_cfg)
    GNUNET_CONFIGURATION_destroy (new_cfg);
  test_finished ();
}
Exemple #16
0
/***
 * load gnunet configuration
 */
  void
gg_load_configuration(GNUNET_gstData * d)
{
  char *audiobackend_string;
  cfg =  GNUNET_CONFIGURATION_create();
  GNUNET_CONFIGURATION_load(cfg, "mediahelper.conf");

  char *section = "MEDIAHELPER";

  GNUNET_CONFIGURATION_get_value_string(cfg, "MEDIAHELPER", "JACK_PP_IN", &d->jack_pp_in);
  GNUNET_CONFIGURATION_get_value_string(cfg, "MEDIAHELPER", "JACK_PP_OUT", &d->jack_pp_out);

  GNUNET_CONFIGURATION_get_value_string(cfg, "MEDIAHELPER", "AUDIOBACKEND", &audiobackend_string);

 // printf("abstring: %s \n", audiobackend_string);

  if ( audiobackend_string == "AUTO" )
  {
    d->audiobackend = AUTO;
  } else if ( audiobackend_string = "JACK" )
  {
    d->audiobackend = JACK;
  } else if ( audiobackend_string = "ALSA" )
  {
    d->audiobackend = ALSA;
  } else if ( audiobackend_string = "FAKE" )
  {
    d->audiobackend = FAKE;
  } else if ( audiobackend_string = "TEST" )
  {
    d->audiobackend = TEST;
  } else
  {
    d->audiobackend = AUTO;
  }

  if (GNUNET_CONFIGURATION_get_value_yesno(cfg, "MEDIAHELPER", "REMOVESILENCE") == GNUNET_YES)
  {
    d->dropsilence = TRUE;
  } else {
    d->dropsilence = FALSE;
  }

  if (GNUNET_CONFIGURATION_get_value_yesno(cfg, "MEDIAHELPER", "NO_GN_HEADERS") == GNUNET_YES)
  {
    d->pure_ogg = TRUE;
  } else {
    d->pure_ogg = FALSE;
  }


  if (GNUNET_CONFIGURATION_get_value_yesno(cfg, "MEDIAHELPER", "USERTP") == GNUNET_YES)
  {
    d->usertp = TRUE;
  } else {
    d->usertp = FALSE;
  }

//  GNUNET_CONFIGURATION_write(cfg, "mediahelper.conf");

}
/**
 * Checking configuration diffs
 */
static int
checkDiffs (struct GNUNET_CONFIGURATION_Handle *cfgDefault, int option)
{
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_CONFIGURATION_Handle *cfgDiffs;
  struct DiffsCBData cbData;
  int ret;
  char *diffsFileName;

  initDiffsCBData (&cbData);

  cfg = GNUNET_CONFIGURATION_create ();
  /* load defaults */
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, NULL));

  /* Modify configuration and save it */
  cfgDiffs = editConfiguration (cfg, option);
  diffsFileName = GNUNET_DISK_mktemp ("gnunet-test-configurations-diffs.conf");
  if (diffsFileName == NULL)
  {
    GNUNET_break (0);
    GNUNET_CONFIGURATION_destroy (cfg);
    GNUNET_CONFIGURATION_destroy (cfgDiffs);
    return 1;
  }
  GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, diffsFileName);
  GNUNET_CONFIGURATION_destroy (cfg);

  /* Compare the dumped configuration with modifications done */
  cfg = GNUNET_CONFIGURATION_create ();
  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, diffsFileName));
  if (0 != remove (diffsFileName))
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", diffsFileName);
  cbData.callBackOption = COMPARE;
  cbData.cfgDiffs = cfgDiffs;
  GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
  if (1 == (ret = cbData.status))
  {
    FPRINTF (stderr, "%s", 
             "Incorrect Configuration Diffs: Diffs may contain data not actually edited\n");
    goto housekeeping;
  }
  cbData.cfgDiffs = cfg;
  GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
  if ((ret = cbData.status) == 1)
    FPRINTF (stderr, "%s", 
             "Incorrect Configuration Diffs: Data may be missing in diffs\n");

housekeeping:
#if 0
  cbData.section = NULL;
  cbData.callBackOption = PRINT;
  printf ("\nExpected Diffs:\n");
  GNUNET_CONFIGURATION_iterate (cfgDiffs, diffsCallBack, &cbData);
  cbData.section = NULL;
  printf ("\nActual Diffs:\n");
  GNUNET_CONFIGURATION_iterate (cfg, diffsCallBack, &cbData);
#endif
  GNUNET_CONFIGURATION_destroy (cfg);
  GNUNET_CONFIGURATION_destroy (cfgDiffs);
  GNUNET_free (diffsFileName);
  return ret;
}
/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *mycfg)
{
  struct GNUNET_CONFIGURATION_Handle *properties;
  char *file;

  end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
  cfg = mycfg;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon starting ... \n");

  if (GNUNET_SYSERR ==GNUNET_CONFIGURATION_get_value_filename (mycfg, "sysmon", "CFGFILE", &file))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sysmon configuration file not set, exit! \n");
    shutdown_now();
    ret = 1;
    return;
  }

  properties = GNUNET_CONFIGURATION_create();
  if (NULL == properties)
  {
    GNUNET_break (0);
    shutdown_now();
    ret = 1;
    return;
  }
  if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (properties, file))
  {
      GNUNET_break (0);
      GNUNET_CONFIGURATION_destroy (properties);
      GNUNET_free (file);
      ret = 1;
      shutdown_now();
      return;
  }
  GNUNET_free (file);
  GNUNET_CONFIGURATION_iterate_sections (properties, &load_property, properties);

  GNUNET_CONFIGURATION_destroy (properties);

  /* Creating statistics */
  stats = GNUNET_STATISTICS_create ("sysmon", mycfg);
  if (NULL == stats)
  {
    GNUNET_break (0);
    shutdown_now();
    ret = 1;
    return;
  }

  /* load properties */
  if (GNUNET_SYSERR == load_default_properties ())
  {
    GNUNET_break (0);
    shutdown_now();
    ret = 1;
    return;
  }

  /* run properties */
  if (GNUNET_SYSERR == run_properties ())
  {
    GNUNET_break (0);
    shutdown_now();
    ret = 1;
    return;
  }

}