Discussion:
[libtorrent] DHT
Michael Mckeown
2016-04-16 16:24:03 UTC
Permalink
I'm using Libtorrent like so:

using namespace libtorrent;
libtorrent::session* Client_Session;

void start_client_session ( )
{

Client_Session = new libtorrent::session;
dht_settings dht;
dht_settings ( );
settings_pack pack;

pack.set_str ( settings_pack::user_agent , std::string ( "Test " )
+ TEST_VERSION_STRING );
pack.set_int ( settings_pack::alert_mask ,
alert::error_notification | alert::storage_notification |
alert::status_notification | alert::dht_notification );
pack.set_bool ( settings_pack::enable_upnp , true );
pack.set_bool ( settings_pack::enable_natpmp , true );
pack.set_bool ( settings_pack::enable_lsd , true );
pack.set_bool ( settings_pack::enable_dht , true );
std::string ip = "0.0.0.0:6881";
pack.set_str ( settings_pack::listen_interfaces , ip );
session Client_Session ( pack );
Client_Session.set_dht_settings ( dht );

std::vector<stats_metric> map = session_stats_metrics ( );
for ( std::vector<stats_metric>::const_iterator i = map.begin ( );
i != map.end ( ); ++i )
{
if ( i->name == "peer.num_peers_connected" )
{
statistics.num_peers_connected = i->value_index;
}
if ( i->name == "dht.dht_nodes" )
{
statistics.dht_nodes = i->value_index;
}
if ( i->name == "net.sent_payload_bytes" )
{
statistics.sent_payload_bytes = i->value_index;
}
if ( i->name == "net.recv_payload_bytes" )
{
statistics.recv_payload_bytes = i->value_index;
}
if ( i->name == "net.has_incoming_connections" )
{
statistics.has_incoming_connections = i->value_index;
}
}
}

on timer:

void timer_a ( )
{

Client_Session->post_session_stats ( );

std::vector<libtorrent::alert*> alerts;
Client_Session->pop_alerts ( &alerts );

for ( libtorrent::alert const* a : alerts )
{

if ( auto se = libtorrent::alert_cast<
libtorrent::session_stats_alert >( a ) )
{
no_dht_nodes = se->values [ statistics.dht_nodes ];
no_peers = se->values [ statistics.num_peers_connected ];
total_uploaded = se->values [ statistics.sent_payload_bytes ];
total_downloaded = se->values [
statistics.recv_payload_bytes ];
}
}
}

The port opens and I can turn on/off things and change settings but as
no torrents are added here and DHT is running I was expecting DHT nodes
to be greater than 0 and still have some
sent_payload_bytes/recv_payload_bytes but this is not the case.

In utorrent with no torrents added you get the no of dht nodes and there
is erroneous upload/download data, what is it I'm doing wrong here?
Arvid Norberg
2016-04-16 16:34:07 UTC
Permalink
On Sat, Apr 16, 2016 at 12:24 PM, Michael Mckeown <
Post by Michael Mckeown
[...]
The port opens and I can turn on/off things and change settings but as
no torrents are added here and DHT is running I was expecting DHT nodes
to be greater than 0 and still have some
sent_payload_bytes/recv_payload_bytes but this is not the case.
You may want to ask yourself where those DHT nodes would come from.

Most nodes come from other nodes, but if you have 0 nodes, you won't get
any nodes that way.

BitTorrent peers are also likely to be DHT nodes, so libtorrent will try
those as DHT nodes as well. But if you don't have any torrents, you won't
get any peers.

You can add nodes by calling session::add_dht_node(), but you're not doing
that.

Another way to get peers is to add a DHT router node (which is just used
for bootstrapping). This is most likely how uTorrent would get its first
peers in the scenario you mention.

For instance, you may want to add ("router.utorrent.com", 6881) as a router
node.
Post by Michael Mckeown
In utorrent with no torrents added you get the no of dht nodes and there
is erroneous upload/download data, what is it I'm doing wrong here?
--
Arvid Norberg
Michael Mckeown
2016-04-16 17:13:12 UTC
Permalink
Thank you, I done this:

std::string r = "router.bittorrent.com"; // or router.utorrent.com
std::pair <std::string , int> pair = std::make_pair ( r , 6881 );
Client_Session.add_dht_router (pair );

and my application window simply does not show and no debugging errors
present, same symptoms as the error I reported with tick_interval, I'm
still trying to debug the tick_interval so perhaps these are related.

Anyhow the dht explanation was most helpful, thanks.
Post by Arvid Norberg
On Sat, Apr 16, 2016 at 12:24 PM, Michael Mckeown <
Post by Michael Mckeown
[...]
The port opens and I can turn on/off things and change settings but as
no torrents are added here and DHT is running I was expecting DHT nodes
to be greater than 0 and still have some
sent_payload_bytes/recv_payload_bytes but this is not the case.
You may want to ask yourself where those DHT nodes would come from.
Most nodes come from other nodes, but if you have 0 nodes, you won't get
any nodes that way.
BitTorrent peers are also likely to be DHT nodes, so libtorrent will try
those as DHT nodes as well. But if you don't have any torrents, you won't
get any peers.
You can add nodes by calling session::add_dht_node(), but you're not doing
that.
Another way to get peers is to add a DHT router node (which is just used
for bootstrapping). This is most likely how uTorrent would get its first
peers in the scenario you mention.
For instance, you may want to add ("router.utorrent.com", 6881) as a router
node.
Post by Michael Mckeown
In utorrent with no torrents added you get the no of dht nodes and there
is erroneous upload/download data, what is it I'm doing wrong here?
Continue reading on narkive:
Loading...