/** * Restart the given peer * @param tth testing handle * @param p the peer * @param cfgname the cfg file used to restart * @param restart_cb callback to call when restarted * @param cb_cls callback closure * @return GNUNET_OK in success otherwise GNUNET_SYSERR */ int GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth, struct PeerContext *p, const char *cfgname, GNUNET_TRANSPORT_TESTING_start_cb restart_cb, void *cb_cls) { GNUNET_assert (tth != NULL); GNUNET_assert (p != NULL); GNUNET_assert (NULL != p->peer); GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing", "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id)); /* shutdown */ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing", "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id)); if (NULL != p->ghh) GNUNET_TRANSPORT_get_hello_cancel (p->ghh); p->ghh = NULL; if (NULL != p->th) GNUNET_TRANSPORT_disconnect (p->th); if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop(p->peer)) { GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing", "Failed to stop peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id)); return GNUNET_SYSERR; } sleep (5); /* restart */ if (GNUNET_SYSERR == GNUNET_TESTING_peer_start(p->peer)) { GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing", "Failed to restart peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id)); return GNUNET_SYSERR; } GNUNET_assert (p->th != NULL); GNUNET_assert (p->start_cb == NULL); p->start_cb = restart_cb; p->cb_cls = cb_cls; p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive, ¬ify_connect, ¬ify_disconnect); GNUNET_assert (NULL != p->th); p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p); GNUNET_assert (p->ghh != NULL); return GNUNET_OK; }
/** * 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) { static struct GNUNET_CORE_MessageHandler handlers[] = { {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0}, {NULL, 0, 0} }; unsigned long long opt; cfg = c; stats = GNUNET_STATISTICS_create ("topology", cfg); friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY"); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS", &opt)) opt = 0; minimum_friend_count = (unsigned int) opt; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "TARGET-CONNECTION-COUNT", &opt)) opt = 16; target_connection_count = (unsigned int) opt; peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2, GNUNET_NO); if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0)) read_friends_file (cfg); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Topology would like %u connections with at least %u friends\n", target_connection_count, minimum_friend_count); if ((friend_count < minimum_friend_count) && (blacklist == NULL)) blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL); transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL); handle = GNUNET_CORE_connect (cfg, NULL, &core_init, &connect_notify, &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers); GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task, NULL); if (NULL == transport) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to connect to `%s' service.\n"), "transport"); GNUNET_SCHEDULER_shutdown (); return; } if (NULL == handle) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to connect to `%s' service.\n"), "core"); GNUNET_SCHEDULER_shutdown (); return; } }
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, ¬ify_connect, NULL); GNUNET_assert (p->th != NULL); p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p); }
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)); p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL); GNUNET_assert (p->th != NULL); p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p); GNUNET_free (binary); }
/** * 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, ¬ify_receive, ¬ify_connect, ¬ify_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; }
static void testservice_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { int counter = 0; ret = 1; if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) { FPRINTF (stderr, _("Service `%s' is not running\n"), "transport"); return; } if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))) { FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid); return; } counter = benchmark_send + benchmark_receive + iterate_connections + monitor_connections + monitor_connects + try_connect; if (1 < counter) { FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"), "connect", "benchmark send", "benchmark receive", "information", "monitor", "events"); return; } if (0 == counter) { FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"), "connect", "benchmark send", "benchmark receive", "information", "monitor", "events"); return; } if (try_connect) /* -C: Connect to peer */ { if (NULL == cpid) { FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"), "-C", "-p"); ret = 1; return; } handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, ¬ify_connect, ¬ify_disconnect); if (NULL == handle) { FPRINTF (stderr, "%s", _("Failed to connect to transport service\n")); ret = 1; return; } tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL); if (NULL == tc_handle) { FPRINTF (stderr, "%s", _("Failed to send request to transport service\n")); ret = 1; return; } op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL); } else if (benchmark_send) /* -s: Benchmark sending */ { if (NULL == cpid) { FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"), "-s", "-p"); ret = 1; return; } handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, ¬ify_connect, ¬ify_disconnect); if (NULL == handle) { FPRINTF (stderr, "%s", _("Failed to connect to transport service\n")); ret = 1; return; } tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL); if (NULL == tc_handle) { FPRINTF (stderr, "%s", _("Failed to send request to transport service\n")); ret = 1; return; } start_time = GNUNET_TIME_absolute_get (); op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL); } else if (benchmark_receive) /* -b: Benchmark receiving */ { handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL, NULL); if (NULL == handle) { FPRINTF (stderr, "%s", _("Failed to connect to transport service\n")); ret = 1; return; } if (verbosity > 0) FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n")); start_time = GNUNET_TIME_absolute_get (); } else if (iterate_connections) /* -i: List all active addresses once */ { peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO); address_resolution_in_progress = GNUNET_YES; pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, (NULL == cpid) ? NULL : &pid, GNUNET_YES, TIMEOUT, &process_address, (void *) cfg); op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout, NULL); } else if (monitor_connections) /* -m: List all active addresses continously */ { peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO); address_resolution_in_progress = GNUNET_YES; pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, (NULL == cpid) ? NULL : &pid, GNUNET_NO, TIMEOUT, &process_address, (void *) cfg); } else if (monitor_connects) /* -e : Monitor (dis)connect events continously */ { monitor_connect_counter = 0; handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, &monitor_notify_connect, &monitor_notify_disconnect); if (NULL == handle) { FPRINTF (stderr, "%s", _("Failed to connect to transport service\n")); ret = 1; return; } ret = 0; } else { GNUNET_break (0); return; } end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, 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) { char *dbfile; struct WhiteListRow *wl_head; struct WhiteListRow *wl_entry; struct GNUNET_PeerIdentity identity; struct GNUNET_ATS_Properties prop; struct GNUNET_TIME_Relative delay; unsigned long long pid; unsigned int nrows; int ret; if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "TESTBED", "PEERID", &pid)) { GNUNET_break (0); return; } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED-UNDERLAY", "DBFILE", &dbfile)) { GNUNET_break (0); return; } if (SQLITE_OK != (ret = sqlite3_open_v2 (dbfile, &db, SQLITE_OPEN_READONLY, NULL))) { if (NULL != db) { LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2"); GNUNET_break (SQLITE_OK == sqlite3_close (db)); } else LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile); GNUNET_free (dbfile); return; } DEBUG ("Opened database %s\n", dbfile); GNUNET_free (dbfile); dbfile = NULL; wl_head = NULL; if (GNUNET_OK != load_keys (c)) goto close_db; transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL); if (NULL == transport) { GNUNET_break (0); return; } /* read and process whitelist */ nrows = 0; wl_head = NULL; nrows = db_read_whitelist (db, pid, &wl_head); if ((GNUNET_SYSERR == nrows) || (0 == nrows)) { GNUNET_TRANSPORT_disconnect (transport); goto close_db; } map = GNUNET_CONTAINER_multipeermap_create (nrows, GNUNET_NO); while (NULL != (wl_entry = wl_head)) { wl_head = wl_entry->next; delay.rel_value_us = wl_entry->latency; memset (&prop, 0, sizeof (prop)); GNUNET_assert (GNUNET_OK == get_identity (wl_entry->id, &identity)); GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (map, &identity, &identity, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); DEBUG ("Setting %u ms latency to peer `%s'\n", wl_entry->latency, GNUNET_i2s (&identity)); GNUNET_TRANSPORT_set_traffic_metric (transport, &identity, &prop, delay, delay); GNUNET_free (wl_entry); } bh = GNUNET_TRANSPORT_blacklist (c, &check_access, NULL); shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown, NULL); close_db: GNUNET_break (SQLITE_OK == sqlite3_close (db)); return; }