made nat-pmp generate alerts as well, just like upnp

This commit is contained in:
Arvid Norberg 2008-10-22 07:06:58 +00:00
parent 49c01d7754
commit 0ed75c3f64
2 changed files with 58 additions and 71 deletions

View File

@ -77,6 +77,7 @@ private:
void update_expiration_timer(); void update_expiration_timer();
void mapping_expired(error_code const& e, int i); void mapping_expired(error_code const& e, int i);
void log(std::string const& msg);
void disable(char const* message); void disable(char const* message);
struct mapping_t struct mapping_t
@ -149,10 +150,6 @@ private:
typedef boost::mutex mutex_t; typedef boost::mutex mutex_t;
mutex_t m_mutex; mutex_t m_mutex;
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
std::ofstream m_log;
#endif
}; };
} }

View File

@ -59,9 +59,6 @@ natpmp::natpmp(io_service& ios, address const& listen_interface, portmap_callbac
, m_next_refresh(-1) , m_next_refresh(-1)
, m_disabled(false) , m_disabled(false)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log.open("natpmp.log", std::ios::in | std::ios::out | std::ios::trunc);
#endif
rebind(listen_interface); rebind(listen_interface);
} }
@ -73,10 +70,9 @@ void natpmp::rebind(address const& listen_interface)
address gateway = get_default_gateway(m_socket.get_io_service(), ec); address gateway = get_default_gateway(m_socket.get_io_service(), ec);
if (ec) if (ec)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() << " failed to find default router: " msg << "failed to find default route: " << ec.message();
<< ec.message() << std::endl; log(msg.str());
#endif
disable("failed to find default router"); disable("failed to find default router");
return; return;
} }
@ -87,10 +83,9 @@ void natpmp::rebind(address const& listen_interface)
if (nat_endpoint == m_nat_endpoint) return; if (nat_endpoint == m_nat_endpoint) return;
m_nat_endpoint = nat_endpoint; m_nat_endpoint = nat_endpoint;
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() << " found router at: " msg << "found router at: " << m_nat_endpoint.address();
<< m_nat_endpoint.address() << std::endl; log(msg.str());
#endif
m_socket.open(udp::v4(), ec); m_socket.open(udp::v4(), ec);
if (ec) if (ec)
@ -119,6 +114,11 @@ void natpmp::rebind(address const& listen_interface)
} }
} }
void natpmp::log(std::string const& msg)
{
m_callback(-1, 0, msg);
}
void natpmp::disable(char const* message) void natpmp::disable(char const* message)
{ {
m_disabled = true; m_disabled = true;
@ -130,10 +130,6 @@ void natpmp::disable(char const* message)
i->protocol = none; i->protocol = none;
m_callback(i - m_mappings.begin(), 0, message); m_callback(i - m_mappings.begin(), 0, message);
} }
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() << " NAT-PMP disabled: " << message << std::endl;
#endif
close(); close();
} }
void natpmp::delete_mapping(int index) void natpmp::delete_mapping(int index)
@ -174,10 +170,7 @@ int natpmp::add_mapping(protocol_type p, int external_port, int local_port)
void natpmp::try_next_mapping(int i) void natpmp::try_next_mapping(int i)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) /*
m_log << time_now_string() << " try_next_mapping [ " << i << " ]" << std::endl;
#endif
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
ptime now = time_now(); ptime now = time_now();
for (std::vector<mapping_t>::iterator m = m_mappings.begin() for (std::vector<mapping_t>::iterator m = m_mappings.begin()
@ -192,7 +185,7 @@ void natpmp::try_next_mapping(int i)
<< " ]" << std::endl; << " ]" << std::endl;
} }
#endif #endif
*/
if (i < int(m_mappings.size()) - 1) if (i < int(m_mappings.size()) - 1)
{ {
update_mapping(i + 1); update_mapping(i + 1);
@ -211,15 +204,15 @@ void natpmp::try_next_mapping(int i)
m_send_timer.cancel(ec); m_send_timer.cancel(ec);
m_socket.close(ec); m_socket.close(ec);
} }
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) //#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << " done" << (m_abort?" shutting down":"") << std::endl; // m_log << " done" << (m_abort?" shutting down":"") << std::endl;
#endif //#endif
return; return;
} }
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) //#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << " updating " << (m - m_mappings.begin()) << std::endl; // m_log << " updating " << (m - m_mappings.begin()) << std::endl;
#endif //#endif
update_mapping(m - m_mappings.begin()); update_mapping(m - m_mappings.begin());
} }
@ -234,9 +227,9 @@ void natpmp::update_mapping(int i)
m_send_timer.cancel(ec); m_send_timer.cancel(ec);
m_socket.close(ec); m_socket.close(ec);
} }
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) //#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << " done" << (m_abort?" shutting down":"") << std::endl; // m_log << " done" << (m_abort?" shutting down":"") << std::endl;
#endif //#endif
return; return;
} }
@ -276,14 +269,12 @@ void natpmp::send_map_request(int i)
int ttl = m.action == mapping_t::action_add ? 3600 : 0; int ttl = m.action == mapping_t::action_add ? 3600 : 0;
write_uint32(ttl, out); // port mapping lifetime write_uint32(ttl, out); // port mapping lifetime
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() msg << "port map [ action: " << (m.action == mapping_t::action_add ? "add" : "delete")
<< " ==> port map ["
<< " action: " << (m.action == mapping_t::action_add ? "add" : "delete") << " "
<< " proto: " << (m.protocol == udp ? "udp" : "tcp") << " proto: " << (m.protocol == udp ? "udp" : "tcp")
<< " local: " << m.local_port << " external: " << m.external_port << " local: " << m.local_port << " external: " << m.external_port
<< " ttl: " << ttl << " ]" << std::endl; << " ttl: " << ttl << " ]";
#endif log(msg.str());
error_code ec; error_code ec;
m_socket.send_to(asio::buffer(buf, 12), m_nat_endpoint, 0, ec); m_socket.send_to(asio::buffer(buf, 12), m_nat_endpoint, 0, ec);
@ -319,11 +310,9 @@ void natpmp::on_reply(error_code const& e
using namespace libtorrent::detail; using namespace libtorrent::detail;
if (e) if (e)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() msg << " error on receiving reply: " << e.message();
<< " <== on_receive [" log(msg.str());
" error: " << e.message() << " ]" << std::endl;
#endif
return; return;
} }
@ -332,11 +321,9 @@ void natpmp::on_reply(error_code const& e
if (m_remote != m_nat_endpoint) if (m_remote != m_nat_endpoint)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() msg << " received packed from wrong IP: " << m_remote;
<< " <== received packet from the wrong IP [" log(msg.str());
" ip: " << m_remote << " ]" << std::endl;
#endif
return; return;
} }
@ -360,34 +347,33 @@ void natpmp::on_reply(error_code const& e
(void)time; // to remove warning (void)time; // to remove warning
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() msg << "port map ["
<< " <== port map ["
<< " protocol: " << (cmd - 128 == 1 ? "udp" : "tcp") << " protocol: " << (cmd - 128 == 1 ? "udp" : "tcp")
<< " local: " << private_port << " external: " << public_port << " local: " << private_port << " external: " << public_port
<< " ttl: " << lifetime << " ]" << std::endl; << " ttl: " << lifetime << " ]";
#endif log(msg.str());
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
if (version != 0) if (version != 0)
{ {
m_log << "*** unexpected version: " << version << std::endl; std::stringstream msg;
msg << "unexpected version: " << version;
log(msg.str());
} }
#endif
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
if (private_port != m.local_port) if (private_port != m.local_port)
{ {
m_log << "*** unexpected local port: " << private_port << std::endl; std::stringstream msg;
msg << "unexpected local port: " << private_port;
log(msg.str());
} }
#endif
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
if (cmd != 128 + m.protocol) if (cmd != 128 + m.protocol)
{ {
m_log << "*** unexpected protocol: " << (cmd - 128) << std::endl; std::stringstream msg;
msg << "unexpected protocol: " << (cmd - 128);
log(msg.str());
} }
#endif
if (public_port == 0 || lifetime == 0) if (public_port == 0 || lifetime == 0)
{ {
@ -403,9 +389,6 @@ void natpmp::on_reply(error_code const& e
if (result != 0) if (result != 0)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << "*** ERROR: " << result << std::endl;
#endif
std::stringstream errmsg; std::stringstream errmsg;
errmsg << "NAT router reports error (" << result << ") "; errmsg << "NAT router reports error (" << result << ") ";
switch (result) switch (result)
@ -436,6 +419,7 @@ void natpmp::update_expiration_timer()
if (m_abort) return; if (m_abort) return;
ptime now = time_now(); ptime now = time_now();
/*
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() << "update_expiration_timer " << std::endl; m_log << time_now_string() << "update_expiration_timer " << std::endl;
for (std::vector<mapping_t>::iterator i = m_mappings.begin() for (std::vector<mapping_t>::iterator i = m_mappings.begin()
@ -450,7 +434,7 @@ void natpmp::update_expiration_timer()
<< " ]" << std::endl; << " ]" << std::endl;
} }
#endif #endif
*/
ptime min_expire = now + seconds(3600); ptime min_expire = now + seconds(3600);
int min_index = -1; int min_index = -1;
for (std::vector<mapping_t>::iterator i = m_mappings.begin() for (std::vector<mapping_t>::iterator i = m_mappings.begin()
@ -470,12 +454,14 @@ void natpmp::update_expiration_timer()
if (min_index >= 0) if (min_index >= 0)
{ {
/*
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() << " next expiration [" m_log << time_now_string() << " next expiration ["
" i: " << min_index " i: " << min_index
<< " ttl: " << total_seconds(min_expire - time_now()) << " ttl: " << total_seconds(min_expire - time_now())
<< " ]" << std::endl; << " ]" << std::endl;
#endif #endif
*/
error_code ec; error_code ec;
if (m_next_refresh >= 0) m_refresh_timer.cancel(ec); if (m_next_refresh >= 0) m_refresh_timer.cancel(ec);
m_refresh_timer.expires_from_now(min_expire - now, ec); m_refresh_timer.expires_from_now(min_expire - now, ec);
@ -488,9 +474,9 @@ void natpmp::mapping_expired(error_code const& e, int i)
{ {
if (e) return; if (e) return;
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) std::stringstream msg;
m_log << time_now_string() << " mapping expired [ i: " << i << " ]" << std::endl; msg << "mapping " << i << " expired";
#endif log(msg.str());
m_mappings[i].action = mapping_t::action_add; m_mappings[i].action = mapping_t::action_add;
if (m_next_refresh == i) m_next_refresh = -1; if (m_next_refresh == i) m_next_refresh = -1;
update_mapping(i); update_mapping(i);
@ -501,14 +487,17 @@ void natpmp::close()
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
m_abort = true; m_abort = true;
error_code ec; error_code ec;
/*
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() << " close" << std::endl; m_log << time_now_string() << " close" << std::endl;
#endif #endif
*/
if (m_disabled) return; if (m_disabled) return;
ptime now = time_now(); ptime now = time_now();
for (std::vector<mapping_t>::iterator i = m_mappings.begin() for (std::vector<mapping_t>::iterator i = m_mappings.begin()
, end(m_mappings.end()); i != end; ++i) , end(m_mappings.end()); i != end; ++i)
{ {
/*
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << " " << (i - m_mappings.begin()) << " [ " m_log << " " << (i - m_mappings.begin()) << " [ "
"proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp") "proto: " << (i->protocol == none ? "none" : i->protocol == tcp ? "tcp" : "udp")
@ -518,6 +507,7 @@ void natpmp::close()
<< " ttl: " << total_seconds(i->expires - now) << " ttl: " << total_seconds(i->expires - now)
<< " ]" << std::endl; << " ]" << std::endl;
#endif #endif
*/
if (i->protocol == none) continue; if (i->protocol == none) continue;
i->action = mapping_t::action_delete; i->action = mapping_t::action_delete;
} }