/** * Function to load keys */ static int load_keys (const struct GNUNET_CONFIGURATION_Handle *c) { char *data_dir; char *idfile; uint64_t fsize; data_dir = NULL; idfile = NULL; fsize = 0; data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); GNUNET_asprintf (&idfile, "%s/testing_hostkeys.ecc", data_dir); GNUNET_free (data_dir); data_dir = NULL; if (GNUNET_OK != GNUNET_DISK_file_size (idfile, &fsize, GNUNET_YES, GNUNET_YES)) { GNUNET_free (idfile); return GNUNET_SYSERR; } if (0 != (fsize % GNUNET_TESTING_HOSTKEYFILESIZE)) { LOG (GNUNET_ERROR_TYPE_ERROR, _("Incorrect hostkey file format: %s\n"), idfile); GNUNET_free (idfile); return GNUNET_SYSERR; } hostkeys_fd = GNUNET_DISK_file_open (idfile, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); if (NULL == hostkeys_fd) { GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", idfile); GNUNET_free (idfile); return GNUNET_SYSERR; } GNUNET_free (idfile); idfile = NULL; hostkeys_data = GNUNET_DISK_file_map (hostkeys_fd, &hostkeys_map, GNUNET_DISK_MAP_TYPE_READ, fsize); if (NULL == hostkeys_data) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mmap"); return GNUNET_SYSERR; } num_hostkeys = fsize / GNUNET_TESTING_HOSTKEYFILESIZE; return GNUNET_OK; }
/** * Load configuration (starts with defaults, then loads * system-specific configuration). * * @param cfg configuration to update * @param filename name of the configuration file, NULL to load defaults * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ int GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename) { char *baseconfig; const char *base_config_varname; base_config_varname = GNUNET_OS_project_data_get ()->base_config_varname; if (NULL != (baseconfig = getenv (base_config_varname))) { baseconfig = GNUNET_strdup (baseconfig); } else { char *ipath; ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); if (NULL == ipath) return GNUNET_SYSERR; GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d"); GNUNET_free (ipath); } if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load_from (cfg, baseconfig)) { GNUNET_free (baseconfig); return GNUNET_SYSERR; /* no configuration at all found */ } GNUNET_free (baseconfig); if ((NULL != filename) && (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename))) { /* specified configuration not found */ return GNUNET_SYSERR; } if (((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) && (filename != NULL)) GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", filename); return GNUNET_OK; }
/** * Start up peerinfo service. * * @param cls closure * @param server the initialized server * @param cfg configuration to use */ static void run (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *cfg) { static const struct GNUNET_SERVER_MessageHandler handlers[] = { {&handle_hello, NULL, GNUNET_MESSAGE_TYPE_HELLO, 0}, {&handle_get, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET, sizeof (struct ListPeerMessage)}, {&handle_get_all, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL, sizeof (struct GNUNET_MessageHeader)}, {&handle_notify, NULL, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY, sizeof (struct GNUNET_MessageHeader)}, {NULL, NULL, 0, 0} }; char *peerdir; char *ip; hostmap = GNUNET_CONTAINER_multihashmap_create (1024); stats = GNUNET_STATISTICS_create ("peerinfo", cfg); notify_list = GNUNET_SERVER_notification_context_create (server, 0); GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, "peerinfo", "HOSTS", &networkIdDirectory)); GNUNET_DISK_directory_create (networkIdDirectory); GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &cron_scan_directory_data_hosts, NULL); GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE, &cron_clean_data_hosts, NULL); GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL); GNUNET_SERVER_add_handlers (server, handlers); ip = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); GNUNET_asprintf (&peerdir, "%shellos", ip); GNUNET_free (ip); GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Importing HELLOs from `%s'\n"), peerdir); GNUNET_DISK_directory_scan (peerdir, &hosts_directory_scan_callback, NULL); GNUNET_free (peerdir); }
static void run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Peer *peer) { enum MHD_FLAG flags; char *bin; char *bin_identity; char *bin_gns; char *config; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "arm", "CONFIG", &config)) { fprintf (stderr, "Failed to locate configuration file. Skipping test.\n"); GNUNET_SCHEDULER_shutdown (); return; } char *const identity_args[] = { "gnunet-identity", "-C", "master-zone", "-c", config, NULL }; char *const identity2_args[] = { "gnunet-identity", "-e", "master-zone", "-s", "gns-master", "-c", config, NULL }; char *const identity3_args[] = { "gnunet-identity", "-e", "master-zone", "-s", "gns-intercept", "-c", config, NULL }; char *const gns_args[] = { "gnunet-gns", "-u", "www.gns", "-c", config, NULL }; GNUNET_TESTING_peer_get_identity (peer, &id); GNUNET_SCHEDULER_add_delayed (TIMEOUT, &do_shutdown, NULL); bin = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR); GNUNET_asprintf (&bin_identity, "%s/%s", bin, "gnunet-identity"); if (0 != fork_and_exec (bin_identity, identity_args)) { fprintf (stderr, "Failed to run `gnunet-identity -C. Skipping test.\n"); GNUNET_SCHEDULER_shutdown (); GNUNET_free (bin_identity); GNUNET_free (config); GNUNET_free (bin); return; } if (0 != fork_and_exec (bin_identity, identity2_args)) { fprintf (stderr, "Failed to run `gnunet-identity -e. Skipping test.\n"); GNUNET_SCHEDULER_shutdown (); GNUNET_free (bin_identity); GNUNET_free (config); GNUNET_free (bin); return; } if (0 != fork_and_exec (bin_identity, identity3_args)) { fprintf (stderr, "Failed to run `gnunet-identity -e. Skipping test.\n"); GNUNET_SCHEDULER_shutdown (); GNUNET_free (bin_identity); GNUNET_free (config); GNUNET_free (bin); return; } GNUNET_free (bin_identity); /* do lookup just to launch GNS service */ GNUNET_asprintf (&bin_gns, "%s/%s", bin, "gnunet-gns"); if (0 != fork_and_exec (bin_gns, gns_args)) { fprintf (stderr, "Failed to run `gnunet-gns -u. Skipping test.\n"); GNUNET_SCHEDULER_shutdown (); GNUNET_free (bin_gns); GNUNET_free (config); GNUNET_free (bin); return; } GNUNET_free (bin_gns); GNUNET_free (config); GNUNET_free (bin); namestore = GNUNET_NAMESTORE_connect (cfg); GNUNET_assert (NULL != namestore); flags = MHD_USE_DEBUG; if (GNUNET_YES == use_v6) flags |= MHD_USE_DUAL_STACK; mhd = MHD_start_daemon (flags, PORT, NULL, NULL, &mhd_ahc, NULL, MHD_OPTION_END); GNUNET_assert (NULL != mhd); mhd_main (); identity = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL); }
/** * Main function that will be run. * * @param cls closure * @param args remaining command-line arguments * @param cfgfile name of the configuration file used (for saving, can be NULL!) * @param c configuration */ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) { struct stat st; char *dir; char *fn; int fd; cfg = c; dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); GNUNET_assert (NULL != dir); GNUNET_asprintf (&fn, "%s%s%s", dir, DIR_SEPARATOR_STR, "gns-bcd.html"); GNUNET_asprintf (&resfile, "%s%s%s", dir, DIR_SEPARATOR_STR, "gns-bcd.tex"); GNUNET_free (dir); fd = OPEN (fn, O_RDONLY); if (-1 == fd) { GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn); GNUNET_free (fn); return; } if (0 != STAT (fn, &st)) { GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn); GNUNET_free (fn); CLOSE (fd); return; } GNUNET_free (fn); if (NULL == (main_response = MHD_create_response_from_fd ((size_t) st.st_size, fd))) { GNUNET_break (0); GNUNET_break (0 == CLOSE (fd)); return; } (void) MHD_add_response_header (main_response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); invalid_gnskey_response = MHD_create_response_from_buffer (strlen (INVALID_GNSKEY), INVALID_GNSKEY, MHD_RESPMEM_PERSISTENT); (void) MHD_add_response_header (invalid_gnskey_response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); not_found_response = MHD_create_response_from_buffer (strlen (NOT_FOUND), NOT_FOUND, MHD_RESPMEM_PERSISTENT); (void) MHD_add_response_header (not_found_response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); if (GNUNET_OK != server_start ()) return; GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &server_stop, NULL); }
/** * 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; }