static void
run (void *cls, char *const *args, const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
  connected = GNUNET_NO;
  blacklist_request_p1 = GNUNET_NO;
  blacklist_request_p2 = GNUNET_NO;

  p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
                                            &notify_receive, &notify_connect,
                                            &notify_disconnect, &start_cb,
                                            NULL);

  p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
                                            &notify_receive, &notify_connect,
                                            &notify_disconnect, &start_cb,
                                            NULL);

  blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
                              &blacklist_cb,
                              p1);

  blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
                              &blacklist_cb,
                              p2);

  GNUNET_assert (blacklist_p1 != NULL);
  GNUNET_assert (blacklist_p2 != 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)
{
  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;
  }
}
/**
 * Method called whenever a peer disconnects.
 *
 * @param cls closure
 * @param peer peer identity this notification is about
 */
static void
disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
{
  struct Peer *pos;

  if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
    return;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Core told us that we disconnected from `%s'\n",
              GNUNET_i2s (peer));
  pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
  if (NULL == pos)
  {
    GNUNET_break (0);
    return;
  }
  if (pos->is_connected != GNUNET_YES)
  {
    GNUNET_break (0);
    return;
  }
  pos->is_connected = GNUNET_NO;
  connection_count--;
  if (NULL != pos->hello_req)
  {
    GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
    pos->hello_req = NULL;
  }
  if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
  {
    GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
    pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
  }
  GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
                         connection_count, GNUNET_NO);
  if (pos->is_friend)
  {
    friend_count--;
    GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
                           friend_count, GNUNET_NO);
  }
  if (((connection_count < target_connection_count) ||
       (friend_count < minimum_friend_count)) &&
      (GNUNET_SCHEDULER_NO_TASK == add_task))
    add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
  if ((friend_count < minimum_friend_count) && (blacklist == NULL))
    blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
}
Beispiel #4
0
/**
 * Method called whenever a peer disconnects.
 *
 * @param cls closure
 * @param peer peer identity this notification is about
 * @param internal_cls the `struct Peer` for this peer
 */
static void
disconnect_notify (void *cls,
                   const struct GNUNET_PeerIdentity *peer,
		   void *internal_cls)
{
  struct Peer *pos = internal_cls;

  if (NULL == pos)
    return; /* myself, we're shutting down */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Core told us that we disconnected from `%s'\n",
              GNUNET_i2s (peer));
  if (NULL == pos->mq)
  {
    GNUNET_break (0);
    return;
  }
  pos->mq = NULL;
  connection_count--;
  if (NULL != pos->hello_delay_task)
  {
    GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
    pos->hello_delay_task = NULL;
  }
  GNUNET_STATISTICS_set (stats,
                         gettext_noop ("# peers connected"),
                         connection_count,
                         GNUNET_NO);
  if (pos->is_friend)
  {
    friend_count--;
    GNUNET_STATISTICS_set (stats,
                           gettext_noop ("# friends connected"),
                           friend_count,
                           GNUNET_NO);
  }
  if ( ( (connection_count < target_connection_count) ||
         (friend_count < minimum_friend_count)) &&
       (NULL == add_task) )
    add_task = GNUNET_SCHEDULER_add_now (&add_peer_task,
                                         NULL);
  if ( (friend_count < minimum_friend_count) &&
       (NULL == blacklist))
    blacklist = GNUNET_TRANSPORT_blacklist (cfg,
                                            &blacklist_check,
                                            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;
}
Beispiel #6
0
/**
 * 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 GNUNET_MQ_MessageHandler handlers[] = {
    GNUNET_MQ_hd_var_size (hello,
                           GNUNET_MESSAGE_TYPE_HELLO,
                           struct GNUNET_HELLO_Message,
                           NULL),
    GNUNET_MQ_handler_end ()
  };
  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_multipeermap_create (target_connection_count * 2,
                                                GNUNET_NO);
  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) &&
       (NULL == blacklist))
    blacklist = GNUNET_TRANSPORT_blacklist (cfg,
                                            &blacklist_check,
                                            NULL);
  ats = GNUNET_ATS_connectivity_init (cfg);
  handle = GNUNET_CORE_connect (cfg,
				NULL,
				&core_init,
				&connect_notify,
				&disconnect_notify,
				handlers);
  GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
				 NULL);
  if (NULL == handle)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Failed to connect to `%s' service.\n"),
                "core");
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}