support a NULL dht_observer again

This commit is contained in:
Arvid Norberg 2015-05-19 00:48:04 +00:00
parent 051ca6b22c
commit d18495eb39
1 changed files with 82 additions and 42 deletions

View File

@ -90,10 +90,13 @@ traversal_algorithm::traversal_algorithm(
, m_timeouts(0) , m_timeouts(0)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
char hex_target[41]; char hex_target[41];
to_hex(reinterpret_cast<char const*>(&target[0]), 20, hex_target); to_hex(reinterpret_cast<char const*>(&target[0]), 20, hex_target);
get_node().observer()->log(dht_logger::traversal, "[%p] NEW target: %s k: %d" get_node().observer()->log(dht_logger::traversal, "[%p] NEW target: %s k: %d"
, this, hex_target, int(m_node.m_table.bucket_size())); , this, hex_target, int(m_node.m_table.bucket_size()));
}
#endif #endif
} }
@ -118,8 +121,11 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
if (ptr == 0) if (ptr == 0)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
get_node().observer()->log(dht_logger::traversal, "[%p] failed to allocate memory or observer. aborting!" get_node().observer()->log(dht_logger::traversal, "[%p] failed to allocate memory or observer. aborting!"
, this); , this);
}
#endif #endif
done(); done();
return; return;
@ -168,11 +174,14 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
// close to this one. We know that it's not the same, because // close to this one. We know that it's not the same, because
// it claims a different node-ID. Ignore this to avoid attacks // it claims a different node-ID. Ignore this to avoid attacks
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
char hex_id[41]; char hex_id[41];
to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id); to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
, "[%p] IGNORING result id: %s addr: %s type: %s" , "[%p] IGNORING result id: %s addr: %s type: %s"
, this, hex_id, print_address(o->target_addr()).c_str(), name()); , this, hex_id, print_address(o->target_addr()).c_str(), name());
}
#endif #endif
return; return;
} }
@ -182,13 +191,17 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
TORRENT_ASSERT((o->flags & observer::flag_no_id) || std::find_if(m_results.begin(), m_results.end() TORRENT_ASSERT((o->flags & observer::flag_no_id) || std::find_if(m_results.begin(), m_results.end()
, boost::bind(&observer::id, _1) == id) == m_results.end()); , boost::bind(&observer::id, _1) == id) == m_results.end());
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
char hex_id[41]; char hex_id[41];
to_hex(reinterpret_cast<char const*>(&id[0]), 20, hex_id); to_hex(reinterpret_cast<char const*>(&id[0]), 20, hex_id);
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
, "[%p] ADD id: %s addr: %s distance: %d invoke-count: %d type: %s" , "[%p] ADD id: %s addr: %s distance: %d invoke-count: %d type: %s"
, this, hex_id, print_endpoint(addr).c_str() , this, hex_id, print_endpoint(addr).c_str()
, distance_exp(m_target, id), m_invoke_count, name()); , distance_exp(m_target, id), m_invoke_count, name());
}
#endif #endif
i = m_results.insert(i, o); i = m_results.insert(i, o);
@ -239,7 +252,7 @@ char const* traversal_algorithm::name() const
void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr) void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (id.is_all_zeros()) if (id.is_all_zeros() && get_node().observer())
{ {
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
, "[%p] WARNING node returned a list which included a node with id 0" , "[%p] WARNING node returned a list which included a node with id 0"
@ -305,6 +318,8 @@ void traversal_algorithm::failed(observer_ptr o, int flags)
++m_branch_factor; ++m_branch_factor;
o->flags |= observer::flag_short_timeout; o->flags |= observer::flag_short_timeout;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
char hex_id[41]; char hex_id[41];
to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id); to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
@ -313,6 +328,7 @@ void traversal_algorithm::failed(observer_ptr o, int flags)
, this, hex_id, distance_exp(m_target, o->id()) , this, hex_id, distance_exp(m_target, o->id())
, print_address(o->target_addr()).c_str(), m_branch_factor , print_address(o->target_addr()).c_str(), m_branch_factor
, m_invoke_count, name()); , m_invoke_count, name());
}
#endif #endif
} }
else else
@ -324,6 +340,8 @@ void traversal_algorithm::failed(observer_ptr o, int flags)
--m_branch_factor; --m_branch_factor;
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
char hex_id[41]; char hex_id[41];
to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id); to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
@ -332,6 +350,7 @@ void traversal_algorithm::failed(observer_ptr o, int flags)
, this, hex_id, distance_exp(m_target, o->id()) , this, hex_id, distance_exp(m_target, o->id())
, print_address(o->target_addr()).c_str(), m_branch_factor , print_address(o->target_addr()).c_str(), m_branch_factor
, m_invoke_count, name()); , m_invoke_count, name());
}
#endif #endif
++m_timeouts; ++m_timeouts;
@ -360,7 +379,7 @@ void traversal_algorithm::done()
, end(m_results.end()); i != end && results_target > 0; ++i) , end(m_results.end()); i != end && results_target > 0; ++i)
{ {
boost::intrusive_ptr<observer> o = *i; boost::intrusive_ptr<observer> o = *i;
if (o->flags & observer::flag_alive) if ((o->flags & observer::flag_alive) && get_node().observer())
{ {
TORRENT_ASSERT(o->flags & observer::flag_queried); TORRENT_ASSERT(o->flags & observer::flag_queried);
char hex_id[41]; char hex_id[41];
@ -376,9 +395,12 @@ void traversal_algorithm::done()
} }
} }
if (get_node().observer())
{
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
, "[%p] COMPLETED distance: %d type: %s" , "[%p] COMPLETED distance: %d type: %s"
, this, closest_target, name()); , this, closest_target, name());
}
#endif #endif
// delete all our references to the observer objects so // delete all our references to the observer objects so
// they will in turn release the traversal algorithm // they will in turn release the traversal algorithm
@ -434,6 +456,8 @@ bool traversal_algorithm::add_requests()
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
char hex_id[41]; char hex_id[41];
to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id); to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
@ -443,6 +467,7 @@ bool traversal_algorithm::add_requests()
, this, int(m_results.end() - i), outstanding, int(m_invoke_count) , this, int(m_results.end() - i), outstanding, int(m_invoke_count)
, int(m_branch_factor), distance_exp(m_target, o->id()), hex_id , int(m_branch_factor), distance_exp(m_target, o->id()), hex_id
, print_address(o->target_addr()).c_str(), name()); , print_address(o->target_addr()).c_str(), name());
}
#endif #endif
o->flags |= observer::flag_queried; o->flags |= observer::flag_queried;
@ -469,9 +494,12 @@ bool traversal_algorithm::add_requests()
void traversal_algorithm::add_router_entries() void traversal_algorithm::add_router_entries()
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
get_node().observer()->log(dht_logger::traversal get_node().observer()->log(dht_logger::traversal
, "[%p] using router nodes to initiate traversal algorithm %d routers" , "[%p] using router nodes to initiate traversal algorithm %d routers"
, this, int(std::distance(m_node.m_table.router_begin(), m_node.m_table.router_end()))); , this, int(std::distance(m_node.m_table.router_begin(), m_node.m_table.router_end())));
}
#endif #endif
for (routing_table::router_iterator i = m_node.m_table.router_begin() for (routing_table::router_iterator i = m_node.m_table.router_begin()
, end(m_node.m_table.router_end()); i != end; ++i) , end(m_node.m_table.router_end()); i != end; ++i)
@ -524,14 +552,19 @@ void traversal_observer::reply(msg const& m)
if (!r) if (!r)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
{
m_algorithm->get_node().observer()->log(dht_logger::traversal m_algorithm->get_node().observer()->log(dht_logger::traversal
, "[%p] missing response dict" , "[%p] missing response dict"
, m_algorithm.get()); , m_algorithm.get());
}
#endif #endif
return; return;
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
{
bdecode_node nid = r.dict_find_string("id"); bdecode_node nid = r.dict_find_string("id");
char hex_id[41]; char hex_id[41];
to_hex(nid.string_ptr(), 20, hex_id); to_hex(nid.string_ptr(), 20, hex_id);
@ -539,6 +572,7 @@ void traversal_observer::reply(msg const& m)
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s" , "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
, m_algorithm.get(), hex_id, m_algorithm->invoke_count() , m_algorithm.get(), hex_id, m_algorithm->invoke_count()
, print_endpoint(target_ep()).c_str(), m_algorithm->name()); , print_endpoint(target_ep()).c_str(), m_algorithm->name());
}
#endif #endif
// look for nodes // look for nodes
bdecode_node n = r.dict_find_string("nodes"); bdecode_node n = r.dict_find_string("nodes");
@ -560,8 +594,11 @@ void traversal_observer::reply(msg const& m)
if (!id || id.string_length() != 20) if (!id || id.string_length() != 20)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (m_algorithm->get_node().observer())
{
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response" m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, m_algorithm.get()); , m_algorithm.get());
}
#endif #endif
return; return;
} }
@ -582,8 +619,11 @@ void traversal_algorithm::abort()
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (get_node().observer())
{
get_node().observer()->log(dht_logger::traversal, "[%p] ABORTED type: %s" get_node().observer()->log(dht_logger::traversal, "[%p] ABORTED type: %s"
, this, name()); , this, name());
}
#endif #endif
done(); done();