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
char hex_target[41]; if (get_node().observer())
to_hex(reinterpret_cast<char const*>(&target[0]), 20, hex_target); {
get_node().observer()->log(dht_logger::traversal, "[%p] NEW target: %s k: %d" char hex_target[41];
, this, hex_target, int(m_node.m_table.bucket_size())); to_hex(reinterpret_cast<char const*>(&target[0]), 20, hex_target);
get_node().observer()->log(dht_logger::traversal, "[%p] NEW target: %s k: %d"
, 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
get_node().observer()->log(dht_logger::traversal, "[%p] failed to allocate memory or observer. aborting!" if (get_node().observer())
, this); {
get_node().observer()->log(dht_logger::traversal, "[%p] failed to allocate memory or observer. aborting!"
, 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
char hex_id[41]; if (get_node().observer())
to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id); {
get_node().observer()->log(dht_logger::traversal char hex_id[41];
, "[%p] IGNORING result id: %s addr: %s type: %s" to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
, this, hex_id, print_address(o->target_addr()).c_str(), name()); get_node().observer()->log(dht_logger::traversal
, "[%p] IGNORING result id: %s addr: %s type: %s"
, 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()
} }
} }
get_node().observer()->log(dht_logger::traversal if (get_node().observer())
, "[%p] COMPLETED distance: %d type: %s" {
, this, closest_target, name()); get_node().observer()->log(dht_logger::traversal
, "[%p] COMPLETED distance: %d type: %s"
, 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,15 +456,18 @@ bool traversal_algorithm::add_requests()
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
char hex_id[41]; if (get_node().observer())
to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id); {
get_node().observer()->log(dht_logger::traversal char hex_id[41];
, "[%p] INVOKE nodes-left: %d top-invoke-count: %d " to_hex(reinterpret_cast<char const*>(&o->id()[0]), 20, hex_id);
"invoke-count: %d branch-factor: %d " get_node().observer()->log(dht_logger::traversal
"distance: %d id: %s addr: %s type: %s" , "[%p] INVOKE nodes-left: %d top-invoke-count: %d "
, this, int(m_results.end() - i), outstanding, int(m_invoke_count) "invoke-count: %d branch-factor: %d "
, int(m_branch_factor), distance_exp(m_target, o->id()), hex_id "distance: %d id: %s addr: %s type: %s"
, print_address(o->target_addr()).c_str(), name()); , this, int(m_results.end() - i), outstanding, int(m_invoke_count)
, int(m_branch_factor), distance_exp(m_target, o->id()), hex_id
, 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
get_node().observer()->log(dht_logger::traversal if (get_node().observer())
, "[%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()))); get_node().observer()->log(dht_logger::traversal
, "[%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())));
}
#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,21 +552,27 @@ void traversal_observer::reply(msg const& m)
if (!r) if (!r)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
m_algorithm->get_node().observer()->log(dht_logger::traversal if (m_algorithm->get_node().observer())
, "[%p] missing response dict" {
, m_algorithm.get()); m_algorithm->get_node().observer()->log(dht_logger::traversal
, "[%p] missing response dict"
, m_algorithm.get());
}
#endif #endif
return; return;
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
bdecode_node nid = r.dict_find_string("id"); if (m_algorithm->get_node().observer())
char hex_id[41]; {
to_hex(nid.string_ptr(), 20, hex_id); bdecode_node nid = r.dict_find_string("id");
m_algorithm->get_node().observer()->log(dht_logger::traversal char hex_id[41];
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s" to_hex(nid.string_ptr(), 20, hex_id);
, m_algorithm.get(), hex_id, m_algorithm->invoke_count() m_algorithm->get_node().observer()->log(dht_logger::traversal
, print_endpoint(target_ep()).c_str(), m_algorithm->name()); , "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
, m_algorithm.get(), hex_id, m_algorithm->invoke_count()
, 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
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response" if (m_algorithm->get_node().observer())
, m_algorithm.get()); {
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
, m_algorithm.get());
}
#endif #endif
return; return;
} }
@ -582,8 +619,11 @@ void traversal_algorithm::abort()
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
get_node().observer()->log(dht_logger::traversal, "[%p] ABORTED type: %s" if (get_node().observer())
, this, name()); {
get_node().observer()->log(dht_logger::traversal, "[%p] ABORTED type: %s"
, this, name());
}
#endif #endif
done(); done();