moved UPNP_LOGGING out of upnp.cpp and added alerts for when upnp logs something

This commit is contained in:
Arvid Norberg 2008-10-22 01:12:14 +00:00
parent 8be621ac7a
commit 49c01d7754
10 changed files with 291 additions and 273 deletions

View File

@ -176,83 +176,84 @@
<li><a class="reference internal" href="#listen-failed-alert" id="id130">listen_failed_alert</a></li>
<li><a class="reference internal" href="#portmap-error-alert" id="id131">portmap_error_alert</a></li>
<li><a class="reference internal" href="#portmap-alert" id="id132">portmap_alert</a></li>
<li><a class="reference internal" href="#file-error-alert" id="id133">file_error_alert</a></li>
<li><a class="reference internal" href="#tracker-announce-alert" id="id134">tracker_announce_alert</a></li>
<li><a class="reference internal" href="#tracker-error-alert" id="id135">tracker_error_alert</a></li>
<li><a class="reference internal" href="#tracker-reply-alert" id="id136">tracker_reply_alert</a></li>
<li><a class="reference internal" href="#dht-reply-alert" id="id137">dht_reply_alert</a></li>
<li><a class="reference internal" href="#tracker-warning-alert" id="id138">tracker_warning_alert</a></li>
<li><a class="reference internal" href="#scrape-reply-alert" id="id139">scrape_reply_alert</a></li>
<li><a class="reference internal" href="#scrape-failed-alert" id="id140">scrape_failed_alert</a></li>
<li><a class="reference internal" href="#url-seed-alert" id="id141">url_seed_alert</a></li>
<li><a class="reference internal" href="#hash-failed-alert" id="id142">hash_failed_alert</a></li>
<li><a class="reference internal" href="#peer-ban-alert" id="id143">peer_ban_alert</a></li>
<li><a class="reference internal" href="#peer-error-alert" id="id144">peer_error_alert</a></li>
<li><a class="reference internal" href="#invalid-request-alert" id="id145">invalid_request_alert</a></li>
<li><a class="reference internal" href="#torrent-finished-alert" id="id146">torrent_finished_alert</a></li>
<li><a class="reference internal" href="#performance-alert" id="id147">performance_alert</a></li>
<li><a class="reference internal" href="#metadata-failed-alert" id="id148">metadata_failed_alert</a></li>
<li><a class="reference internal" href="#metadata-received-alert" id="id149">metadata_received_alert</a></li>
<li><a class="reference internal" href="#fastresume-rejected-alert" id="id150">fastresume_rejected_alert</a></li>
<li><a class="reference internal" href="#peer-blocked-alert" id="id151">peer_blocked_alert</a></li>
<li><a class="reference internal" href="#storage-moved-alert" id="id152">storage_moved_alert</a></li>
<li><a class="reference internal" href="#torrent-paused-alert" id="id153">torrent_paused_alert</a></li>
<li><a class="reference internal" href="#torrent-resumed-alert" id="id154">torrent_resumed_alert</a></li>
<li><a class="reference internal" href="#save-resume-data-alert" id="id155">save_resume_data_alert</a></li>
<li><a class="reference internal" href="#save-resume-data-failed-alert" id="id156">save_resume_data_failed_alert</a></li>
<li><a class="reference internal" href="#dht-announce-alert" id="id157">dht_announce_alert</a></li>
<li><a class="reference internal" href="#dht-get-peers-alert" id="id158">dht_get_peers_alert</a></li>
<li><a class="reference internal" href="#dispatcher" id="id159">dispatcher</a></li>
<li><a class="reference internal" href="#portmap-log-alert" id="id133">portmap_log_alert</a></li>
<li><a class="reference internal" href="#file-error-alert" id="id134">file_error_alert</a></li>
<li><a class="reference internal" href="#tracker-announce-alert" id="id135">tracker_announce_alert</a></li>
<li><a class="reference internal" href="#tracker-error-alert" id="id136">tracker_error_alert</a></li>
<li><a class="reference internal" href="#tracker-reply-alert" id="id137">tracker_reply_alert</a></li>
<li><a class="reference internal" href="#dht-reply-alert" id="id138">dht_reply_alert</a></li>
<li><a class="reference internal" href="#tracker-warning-alert" id="id139">tracker_warning_alert</a></li>
<li><a class="reference internal" href="#scrape-reply-alert" id="id140">scrape_reply_alert</a></li>
<li><a class="reference internal" href="#scrape-failed-alert" id="id141">scrape_failed_alert</a></li>
<li><a class="reference internal" href="#url-seed-alert" id="id142">url_seed_alert</a></li>
<li><a class="reference internal" href="#hash-failed-alert" id="id143">hash_failed_alert</a></li>
<li><a class="reference internal" href="#peer-ban-alert" id="id144">peer_ban_alert</a></li>
<li><a class="reference internal" href="#peer-error-alert" id="id145">peer_error_alert</a></li>
<li><a class="reference internal" href="#invalid-request-alert" id="id146">invalid_request_alert</a></li>
<li><a class="reference internal" href="#torrent-finished-alert" id="id147">torrent_finished_alert</a></li>
<li><a class="reference internal" href="#performance-alert" id="id148">performance_alert</a></li>
<li><a class="reference internal" href="#metadata-failed-alert" id="id149">metadata_failed_alert</a></li>
<li><a class="reference internal" href="#metadata-received-alert" id="id150">metadata_received_alert</a></li>
<li><a class="reference internal" href="#fastresume-rejected-alert" id="id151">fastresume_rejected_alert</a></li>
<li><a class="reference internal" href="#peer-blocked-alert" id="id152">peer_blocked_alert</a></li>
<li><a class="reference internal" href="#storage-moved-alert" id="id153">storage_moved_alert</a></li>
<li><a class="reference internal" href="#torrent-paused-alert" id="id154">torrent_paused_alert</a></li>
<li><a class="reference internal" href="#torrent-resumed-alert" id="id155">torrent_resumed_alert</a></li>
<li><a class="reference internal" href="#save-resume-data-alert" id="id156">save_resume_data_alert</a></li>
<li><a class="reference internal" href="#save-resume-data-failed-alert" id="id157">save_resume_data_failed_alert</a></li>
<li><a class="reference internal" href="#dht-announce-alert" id="id158">dht_announce_alert</a></li>
<li><a class="reference internal" href="#dht-get-peers-alert" id="id159">dht_get_peers_alert</a></li>
<li><a class="reference internal" href="#dispatcher" id="id160">dispatcher</a></li>
</ul>
</li>
<li><a class="reference internal" href="#exceptions" id="id160">exceptions</a><ul>
<li><a class="reference internal" href="#invalid-handle" id="id161">invalid_handle</a></li>
<li><a class="reference internal" href="#duplicate-torrent" id="id162">duplicate_torrent</a></li>
<li><a class="reference internal" href="#invalid-encoding" id="id163">invalid_encoding</a></li>
<li><a class="reference internal" href="#type-error" id="id164">type_error</a></li>
<li><a class="reference internal" href="#invalid-torrent-file" id="id165">invalid_torrent_file</a></li>
<li><a class="reference internal" href="#exceptions" id="id161">exceptions</a><ul>
<li><a class="reference internal" href="#invalid-handle" id="id162">invalid_handle</a></li>
<li><a class="reference internal" href="#duplicate-torrent" id="id163">duplicate_torrent</a></li>
<li><a class="reference internal" href="#invalid-encoding" id="id164">invalid_encoding</a></li>
<li><a class="reference internal" href="#type-error" id="id165">type_error</a></li>
<li><a class="reference internal" href="#invalid-torrent-file" id="id166">invalid_torrent_file</a></li>
</ul>
</li>
<li><a class="reference internal" href="#storage-interface" id="id166">storage_interface</a><ul>
<li><a class="reference internal" href="#initialize" id="id167">initialize()</a></li>
<li><a class="reference internal" href="#read" id="id168">read()</a></li>
<li><a class="reference internal" href="#write" id="id169">write()</a></li>
<li><a class="reference internal" href="#id12" id="id170">move_storage()</a></li>
<li><a class="reference internal" href="#verify-resume-data" id="id171">verify_resume_data()</a></li>
<li><a class="reference internal" href="#write-resume-data" id="id172">write_resume_data()</a></li>
<li><a class="reference internal" href="#move-slot" id="id173">move_slot()</a></li>
<li><a class="reference internal" href="#swap-slots" id="id174">swap_slots()</a></li>
<li><a class="reference internal" href="#swap-slots3" id="id175">swap_slots3()</a></li>
<li><a class="reference internal" href="#hash-for-slot" id="id176">hash_for_slot()</a></li>
<li><a class="reference internal" href="#rename-file" id="id177">rename_file()</a></li>
<li><a class="reference internal" href="#release-files" id="id178">release_files()</a></li>
<li><a class="reference internal" href="#delete-files" id="id179">delete_files()</a></li>
<li><a class="reference internal" href="#storage-interface" id="id167">storage_interface</a><ul>
<li><a class="reference internal" href="#initialize" id="id168">initialize()</a></li>
<li><a class="reference internal" href="#read" id="id169">read()</a></li>
<li><a class="reference internal" href="#write" id="id170">write()</a></li>
<li><a class="reference internal" href="#id12" id="id171">move_storage()</a></li>
<li><a class="reference internal" href="#verify-resume-data" id="id172">verify_resume_data()</a></li>
<li><a class="reference internal" href="#write-resume-data" id="id173">write_resume_data()</a></li>
<li><a class="reference internal" href="#move-slot" id="id174">move_slot()</a></li>
<li><a class="reference internal" href="#swap-slots" id="id175">swap_slots()</a></li>
<li><a class="reference internal" href="#swap-slots3" id="id176">swap_slots3()</a></li>
<li><a class="reference internal" href="#hash-for-slot" id="id177">hash_for_slot()</a></li>
<li><a class="reference internal" href="#rename-file" id="id178">rename_file()</a></li>
<li><a class="reference internal" href="#release-files" id="id179">release_files()</a></li>
<li><a class="reference internal" href="#delete-files" id="id180">delete_files()</a></li>
</ul>
</li>
<li><a class="reference internal" href="#magnet-links" id="id180">magnet links</a></li>
<li><a class="reference internal" href="#queuing" id="id181">queuing</a><ul>
<li><a class="reference internal" href="#downloading" id="id182">downloading</a></li>
<li><a class="reference internal" href="#seeding" id="id183">seeding</a></li>
<li><a class="reference internal" href="#magnet-links" id="id181">magnet links</a></li>
<li><a class="reference internal" href="#queuing" id="id182">queuing</a><ul>
<li><a class="reference internal" href="#downloading" id="id183">downloading</a></li>
<li><a class="reference internal" href="#seeding" id="id184">seeding</a></li>
</ul>
</li>
<li><a class="reference internal" href="#fast-resume" id="id184">fast resume</a><ul>
<li><a class="reference internal" href="#file-format" id="id185">file format</a></li>
<li><a class="reference internal" href="#fast-resume" id="id185">fast resume</a><ul>
<li><a class="reference internal" href="#file-format" id="id186">file format</a></li>
</ul>
</li>
<li><a class="reference internal" href="#threads" id="id186">threads</a></li>
<li><a class="reference internal" href="#storage-allocation" id="id187">storage allocation</a><ul>
<li><a class="reference internal" href="#sparse-allocation" id="id188">sparse allocation</a></li>
<li><a class="reference internal" href="#full-allocation" id="id189">full allocation</a></li>
<li><a class="reference internal" href="#compact-allocation" id="id190">compact allocation</a></li>
<li><a class="reference internal" href="#threads" id="id187">threads</a></li>
<li><a class="reference internal" href="#storage-allocation" id="id188">storage allocation</a><ul>
<li><a class="reference internal" href="#sparse-allocation" id="id189">sparse allocation</a></li>
<li><a class="reference internal" href="#full-allocation" id="id190">full allocation</a></li>
<li><a class="reference internal" href="#compact-allocation" id="id191">compact allocation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#extensions" id="id191">extensions</a><ul>
<li><a class="reference internal" href="#metadata-from-peers" id="id192">metadata from peers</a></li>
<li><a class="reference internal" href="#http-seeding" id="id193">HTTP seeding</a></li>
<li><a class="reference internal" href="#extensions" id="id192">extensions</a><ul>
<li><a class="reference internal" href="#metadata-from-peers" id="id193">metadata from peers</a></li>
<li><a class="reference internal" href="#http-seeding" id="id194">HTTP seeding</a></li>
</ul>
</li>
<li><a class="reference internal" href="#filename-checks" id="id194">filename checks</a></li>
<li><a class="reference internal" href="#acknowledgments" id="id195">acknowledgments</a></li>
<li><a class="reference internal" href="#filename-checks" id="id195">filename checks</a></li>
<li><a class="reference internal" href="#acknowledgments" id="id196">acknowledgments</a></li>
</ul>
</div>
<div class="section" id="overview">
@ -3991,6 +3992,21 @@ struct portmap_alert: alert
};
</pre>
</div>
<div class="section" id="portmap-log-alert">
<h2>portmap_log_alert</h2>
<p>This alert is generated to log informational events related to either
UPnP or NAT-PMP. They contain a log line and the type (0 = NAT-PMP
and 1 = UPnP). Displaying these messages to an end user is only useful
for debugging the UPnP or NAT-PMP implementation.</p>
<pre class="literal-block">
struct portmap_log_alert: alert
{
//...
int type;
std::string msg;
};
</pre>
</div>
<div class="section" id="file-error-alert">
<h2>file_error_alert</h2>
<p>If the storage fails to read or write files that it needs access to, this alert is

View File

@ -4072,6 +4072,23 @@ the index returned from add_mapping_.
int type;
};
portmap_log_alert
-----------------
This alert is generated to log informational events related to either
UPnP or NAT-PMP. They contain a log line and the type (0 = NAT-PMP
and 1 = UPnP). Displaying these messages to an end user is only useful
for debugging the UPnP or NAT-PMP implementation.
::
struct portmap_log_alert: alert
{
//...
int type;
std::string msg;
};
file_error_alert
----------------

View File

@ -1103,6 +1103,29 @@ namespace libtorrent
}
};
struct TORRENT_EXPORT portmap_log_alert: alert
{
portmap_log_alert(int t, std::string const& m)
: type(t), msg(m)
{}
int type;
std::string msg;
virtual std::auto_ptr<alert> clone() const
{ return std::auto_ptr<alert>(new portmap_log_alert(*this)); }
virtual char const* what() const { return "port map log"; }
const static int static_category = alert::port_mapping_notification;
virtual int category() const { return static_category; }
virtual std::string message() const
{
static char const* type_str[] = {"NAT-PMP", "UPnP"};
std::stringstream ret;
ret << type_str[type] << ": " << msg;
return ret.str();
}
};
struct TORRENT_EXPORT fastresume_rejected_alert: torrent_alert
{
fastresume_rejected_alert(torrent_handle const& h

View File

@ -605,6 +605,9 @@ namespace libtorrent
boost::shared_ptr<logger> m_logger;
private:
#endif
#ifdef TORRENT_UPNP_LOGGING
std::ofstream m_upnp_log;
#endif
address m_external_address;

View File

@ -65,6 +65,7 @@ namespace libtorrent
void send(char const* buffer, int size, error_code& ec);
void close();
int num_send_sockets() const { return m_unicast_sockets.size(); }
private:

View File

@ -62,6 +62,8 @@ namespace libtorrent
// int: external port
// std::string: error message
// an empty string as error means success
// a port-mapping index of -1 means it's
// an informational log message
typedef boost::function<void(int, int, std::string const&)> portmap_callback_t;
class upnp : public intrusive_ptr_base<upnp>
@ -117,6 +119,7 @@ private:
void disable(char const* msg);
void return_error(int mapping, int code);
void log(std::string const&);
void delete_port_mapping(rootdevice& d, int i);
void create_port_mapping(http_connection& c, rootdevice& d, int i);
@ -272,10 +275,6 @@ private:
mutex_t m_mutex;
std::string m_model;
#ifdef TORRENT_UPNP_LOGGING
std::ofstream m_log;
#endif
};
}

View File

@ -219,9 +219,6 @@ namespace libtorrent
if (ec) return;
s->bind(udp::endpoint(addr, 0), ec);
if (ec) return;
if (addr.is_v4())
s->set_option(outbound_interface(addr.to_v4()), ec);
if (ec) return;
m_unicast_sockets.push_back(socket_entry(s));
socket_entry& se = m_unicast_sockets.back();
s->async_receive_from(asio::buffer(se.buffer, sizeof(se.buffer))

View File

@ -566,9 +566,10 @@ namespace libtorrent
{
case alert::debug: m = alert::all_categories; break;
case alert::info: m = alert::all_categories & ~(alert::debug_notification
| alert::progress_notification); break;
| alert::progress_notification | alert::dht_notification); break;
case alert::warning: m = alert::all_categories & ~(alert::debug_notification
| alert::status_notification | alert::progress_notification); break;
| alert::status_notification | alert::progress_notification
| alert::dht_notification); break;
case alert::critical: m = alert::error_notification | alert::storage_notification; break;
case alert::fatal: m = alert::error_notification; break;
default: break;

View File

@ -212,6 +212,9 @@ namespace aux {
m_bandwidth_manager[peer_connection::download_channel] = &m_download_channel;
m_bandwidth_manager[peer_connection::upload_channel] = &m_upload_channel;
#ifdef TORRENT_UPNP_LOGGING
m_upnp_log.open("upnp.log", std::ios::in | std::ios::out | std::ios::trunc);
#endif
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
m_logger = create_log("main_session", listen_port(), false);
(*m_logger) << time_now_string() << "\n";
@ -1990,6 +1993,20 @@ namespace aux {
void session_impl::on_port_mapping(int mapping, int port
, std::string const& errmsg, int map_transport)
{
TORRENT_ASSERT(map_transport >= 0 && map_transport <= 1);
// log message
if (mapping == -1)
{
#ifdef TORRENT_UPNP_LOGGING
char const* transport_names[] = {"NAT-PMP", "UPnP"};
m_upnp_log << time_now_string() << " "
<< transport_names[map_transport] << ": " << errmsg;
#endif
if (m_alerts.should_post<portmap_log_alert>())
m_alerts.post_alert(portmap_log_alert(map_transport, errmsg));
return;
}
#ifndef TORRENT_DISABLE_DHT
if (mapping == m_udp_mapping[map_transport] && port != 0)
{

View File

@ -52,10 +52,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/thread/mutex.hpp>
#include <cstdlib>
#if (defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)) && !defined(TORRENT_UPNP_LOGGING)
#define TORRENT_UPNP_LOGGING
#endif
using boost::bind;
using namespace libtorrent;
@ -75,9 +71,7 @@ upnp::upnp(io_service& ios, connection_queue& cc
, m_ignore_non_routers(ignore_nonrouters)
, m_cc(cc)
{
#ifdef TORRENT_UPNP_LOGGING
m_log.open("upnp.log", std::ios::in | std::ios::out | std::ios::trunc);
#endif
TORRENT_ASSERT(cb);
m_retry_count = 0;
if (state)
@ -108,10 +102,17 @@ upnp::~upnp()
void upnp::discover_device()
{
mutex_t::scoped_lock l(m_mutex);
if (m_socket.num_send_sockets() == 0)
log("No network interfaces to broadcast to");
discover_device_impl();
}
void upnp::log(std::string const& msg)
{
m_callback(-1, 0, msg);
}
void upnp::discover_device_impl()
{
const char msearch[] =
@ -131,11 +132,9 @@ void upnp::discover_device_impl()
if (ec)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " ==> Broadcast FAILED: " << ec.message() << std::endl
<< "aborting" << std::endl;
#endif
std::stringstream msg;
msg << "broadcast failed: " << ec.message() << ". Aborting.";
log(msg.str());
disable(ec.message().c_str());
return;
}
@ -145,10 +144,7 @@ void upnp::discover_device_impl()
m_broadcast_timer.async_wait(bind(&upnp::resend_request
, self(), _1));
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " ==> Broadcasting search for rootdevice" << std::endl;
#endif
log("broadcasting search for rootdevice");
}
// returns a reference to a mapping or -1 on failure
@ -156,14 +152,12 @@ int upnp::add_mapping(upnp::protocol_type p, int external_port, int local_port)
{
mutex_t::scoped_lock l(m_mutex);
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " *** add mapping [ proto: " << (p == tcp?"tcp":"udp")
std::stringstream msg;
msg << "adding port map: [ protocol: " << (p == tcp?"tcp":"udp")
<< " ext_port: " << external_port
<< " local_port :" << local_port << " ]";
if (m_disabled) m_log << " DISABLED";
m_log << std::endl;
#endif
if (m_disabled) msg << " DISABLED";
log(msg.str());
if (m_disabled) return -1;
std::vector<global_mapping_t>::iterator i = std::find_if(
@ -211,13 +205,11 @@ void upnp::delete_mapping(int mapping)
global_mapping_t& m = m_mappings[mapping];
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " *** delete mapping [ proto: " << (m.protocol == tcp?"tcp":"udp")
<< " ext_port:" << m.external_port
<< " local_port:" << m.local_port << " ]";
m_log << std::endl;
#endif
std::stringstream msg;
msg << "deleting port map: [ protocol: " << (m.protocol == tcp?"tcp":"udp")
<< " ext_port: " << m.external_port
<< " local_port :" << m.local_port << " ]";
log(msg.str());
if (m.protocol == none) return;
@ -249,12 +241,7 @@ void upnp::resend_request(error_code const& e)
if (m_devices.empty())
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " *** Got no response in 12 retries. Giving up, "
"disabling UPnP." << std::endl;
#endif
disable("no UPnP router found");
disable("no UPnP router found (no response)");
return;
}
@ -269,10 +256,9 @@ void upnp::resend_request(error_code const& e)
TORRENT_ASSERT(d.magic == 1337);
try
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " ==> connecting to " << d.url << std::endl;
#endif
std::stringstream msg;
msg << "connecting to: " << d.url;
log(msg.str());
if (d.upnp_connection) d.upnp_connection->close();
d.upnp_connection.reset(new http_connection(m_io_service
, m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2
@ -282,11 +268,9 @@ void upnp::resend_request(error_code const& e)
catch (std::exception& e)
{
(void)e;
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " *** Connection failed to: " << d.url
<< " " << e.what() << std::endl;
#endif
std::stringstream msg;
msg << "connection failed to: " << d.url << " " << e.what();
log(msg.str());
d.disabled = true;
}
}
@ -329,25 +313,25 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
error_code ec;
if (!in_local_network(m_io_service, from.address(), ec))
{
#ifdef TORRENT_UPNP_LOGGING
if (ec)
{
m_log << time_now_string() << " <== (" << from << ") error: "
<< ec.message() << std::endl;
std::stringstream msg;
msg << "when receiving response from: " << from << ": " << ec.message();
log(msg.str());
}
else
{
m_log << time_now_string() << " <== (" << from << ") UPnP device "
"ignored because it's not on our local network ";
std::stringstream msg;
msg << "ignoring response from: " << from << ". IP is not on local network. ";
std::vector<ip_interface> net = enum_net_interfaces(m_io_service, ec);
for (std::vector<ip_interface>::const_iterator i = net.begin()
, end(net.end()); i != end; ++i)
{
m_log << "(" << i->interface_address << ", " << i->netmask << ") ";
msg << "(" << i->interface_address << ", " << i->netmask << ") ";
}
m_log << std::endl;
log(msg.str());
}
#endif
return;
}
@ -357,24 +341,23 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
{
// this upnp device is filtered because it's not in the
// list of configured routers
#ifdef TORRENT_UPNP_LOGGING
if (ec)
{
m_log << time_now_string() << " <== (" << from << ") error: "
<< ec.message() << std::endl;
std::stringstream msg;
msg << "when receiving response from: " << from << ": " << ec.message();
log(msg.str());
}
else
{
m_log << time_now_string() << " <== (" << from << ") UPnP device "
"ignored because it's not a router on our network ";
std::stringstream msg;
msg << "ignoring response from: " << from << ". IP is not a router. ";
for (std::vector<ip_route>::const_iterator i = routes.begin()
, end(routes.end()); i != end; ++i)
{
m_log << "(" << i->gateway << ", " << i->netmask << ") ";
msg << "(" << i->gateway << ", " << i->netmask << ") ";
}
m_log << std::endl;
log(msg.str());
}
#endif
return;
}
@ -384,46 +367,43 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
, buffer + bytes_transferred), error);
if (error)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string() << " <== (" << from << ") Rootdevice "
"responded with incorrect HTTP packet. Ignoring device" << std::endl;
#endif
std::stringstream msg;
msg << "received malformed HTTP from " << from;
log(msg.str());
return;
}
if (p.status_code() != 200 && p.method() != "notify")
{
#ifdef TORRENT_UPNP_LOGGING
if (p.method().empty())
m_log << time_now_string()
<< " <== (" << from << ") Device responded with HTTP status: " << p.status_code()
<< ". Ignoring device" << std::endl;
{
std::stringstream msg;
msg << "HTTP status " << p.status_code() << " from " << from;
log(msg.str());
}
else
m_log << time_now_string()
<< " <== (" << from << ") Device with HTTP method: " << p.method()
<< ". Ignoring device" << std::endl;
#endif
{
std::stringstream msg;
msg << "HTTP method " << p.method() << " from " << from;
log(msg.str());
}
return;
}
if (!p.header_finished())
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Rootdevice responded with incomplete HTTP "
"packet. Ignoring device" << std::endl;
#endif
std::stringstream msg;
msg << "incomplete HTTP packet from " << from;
log(msg.str());
return;
}
std::string url = p.header("location");
if (url.empty())
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Rootdevice response is missing a location header. "
"Ignoring device" << std::endl;
#endif
std::stringstream msg;
msg << "missing location header from " << from;
log(msg.str());
return;
}
@ -444,11 +424,9 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
if (error)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Rootdevice advertized an invalid url: '" << d.url
<< "'. " << error << ". Ignoring device" << std::endl;
#endif
std::stringstream msg;
msg << "invalid URL " << d.url << " from " << from << ": " << error;
log(msg.str());
return;
}
@ -457,36 +435,29 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
if (protocol != "http")
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Rootdevice uses unsupported protocol: '" << protocol
<< "'. Ignoring device" << std::endl;
#endif
std::stringstream msg;
msg << "unsupported protocol " << protocol << " from " << from;
log(msg.str());
return;
}
if (d.port == 0)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Rootdevice responded with a url with port 0. "
"Ignoring device" << std::endl;
#endif
std::stringstream msg;
msg << "URL with port 0 from " << from;
log(msg.str());
return;
}
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Found rootdevice: " << d.url
<< " total: " << m_devices.size() << std::endl;
#endif
std::stringstream msg;
msg << "found rootdevice: " << d.url << ". (" << m_devices.size() << ")";
log(msg.str());
if (m_devices.size() >= 50)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << from << ") Too many devices (" << m_devices.size() << "), "
"ignoring: " << d.url << std::endl;
#endif
std::stringstream msg;
msg << "too many devices (" << m_devices.size() << "). Ignoring " << d.url;
log(msg.str());
return;
}
@ -525,10 +496,10 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
try
{
#endif
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " ==> connecting to " << d.url << std::endl;
#endif
std::stringstream msg;
msg << "connecting to: " << d.url;
log(msg.str());
if (d.upnp_connection) d.upnp_connection->close();
d.upnp_connection.reset(new http_connection(m_io_service
, m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2
@ -539,11 +510,9 @@ void upnp::on_reply(udp::endpoint const& from, char* buffer
catch (std::exception& e)
{
(void)e;
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " *** Connection failed to: " << d.url
<< " " << e.what() << std::endl;
#endif
std::stringstream msg;
msg << "connection failed to: " << d.url << " " << e.what();
log(msg.str());
d.disabled = true;
}
#endif
@ -568,11 +537,9 @@ void upnp::post(upnp::rootdevice const& d, std::string const& soap
d.upnp_connection->sendbuffer = header.str();
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " ==> sending: " << header.str() << std::endl;
#endif
std::stringstream msg;
msg << "sending: " << header.str();
log(msg.str());
}
void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i)
@ -584,10 +551,9 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i)
if (!d.upnp_connection)
{
TORRENT_ASSERT(d.disabled);
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string() << " *** mapping (" << i
<< ") aborted" << std::endl;
#endif
std::stringstream msg;
msg << "mapping " << i << " aborted";
log(msg.str());
return;
}
@ -644,11 +610,9 @@ void upnp::update_map(rootdevice& d, int i)
if (m.action == mapping_t::action_none
|| m.protocol == none)
{
#ifdef TORRENT_UPNP_LOGGING
if (m.protocol != none)
m_log << time_now_string() << " *** mapping (" << i
<< ") does not need update, skipping" << std::endl;
#endif
std::stringstream msg;
msg << "mapping " << i << " does not need updating, skipping";
log(msg.str());
next(d, i);
return;
}
@ -656,10 +620,9 @@ void upnp::update_map(rootdevice& d, int i)
TORRENT_ASSERT(!d.upnp_connection);
TORRENT_ASSERT(d.service_namespace);
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " ==> connecting to " << d.hostname << std::endl;
#endif
std::stringstream msg;
msg << "connecting to " << d.hostname;
log(msg.str());
if (m.action == mapping_t::action_add)
{
if (m.failcount > 5)
@ -701,10 +664,9 @@ void upnp::delete_port_mapping(rootdevice& d, int i)
if (!d.upnp_connection)
{
TORRENT_ASSERT(d.disabled);
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string() << " *** unmapping (" << i
<< ") aborted" << std::endl;
#endif
std::stringstream msg;
msg << "unmapping " << i << " aborted";
log(msg.str());
return;
}
@ -806,31 +768,27 @@ void upnp::on_upnp_xml(error_code const& e
if (e && e != asio::error::eof)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << d.url << ") error while fetching control url: "
<< e.message() << std::endl;
#endif
std::stringstream msg;
msg << "error while fetching control url from " << d.url << ": " << e.message();
log(msg.str());
d.disabled = true;
return;
}
if (!p.header_finished())
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << d.url << ") error while fetching control url: incomplete http message" << std::endl;
#endif
std::stringstream msg;
msg << "error while fetching control url from " << d.url << ": incomplete HTTP message";
log(msg.str());
d.disabled = true;
return;
}
if (p.status_code() != 200)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << d.url << ") error while fetching control url: " << p.message() << std::endl;
#endif
std::stringstream msg;
msg << "error while fetching control url from " << d.url << ": " << p.message();
log(msg.str());
d.disabled = true;
return;
}
@ -858,21 +816,18 @@ void upnp::on_upnp_xml(error_code const& e
}
else
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << d.url << ") Rootdevice response, did not find "
"a port mapping interface" << std::endl;
#endif
std::stringstream msg;
msg << "Could not find a port mapping interface in response from " << d.url;
log(msg.str());
d.disabled = true;
return;
}
}
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== (" << d.url << ") Rootdevice response, found control URL: " << s.control_url
<< " namespace: " << d.service_namespace << std::endl;
#endif
std::stringstream msg;
msg << "found control URL: " << s.control_url << " namespace: "
<< d.service_namespace << " in response from " << d.url;
log(msg.str());
d.control_url = s.control_url;
@ -884,10 +839,9 @@ void upnp::on_upnp_xml(error_code const& e
if (error)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " *** Failed to parse URL '" << d.control_url << "': " << error << std::endl;
#endif
std::stringstream msg;
msg << "failed to parse URL '" << d.control_url << "': " << error;
log(msg.str());
d.disabled = true;
return;
}
@ -982,10 +936,9 @@ void upnp::on_upnp_map_response(error_code const& e
if (e && e != asio::error::eof)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== error while adding portmap: " << e.message() << std::endl;
#endif
std::stringstream msg;
msg << "error while adding port map: " << e.message();
log(msg.str());
d.disabled = true;
return;
}
@ -1011,10 +964,9 @@ void upnp::on_upnp_map_response(error_code const& e
if (!p.header_finished())
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== error while adding portmap: incomplete http message" << std::endl;
#endif
std::stringstream msg;
msg << "error while adding port map: incomplete http message";
log(msg.str());
next(d, mapping);
return;
}
@ -1026,13 +978,12 @@ void upnp::on_upnp_map_response(error_code const& e
xml_parse((char*)p.get_body().begin, (char*)p.get_body().end
, bind(&find_error_code, _1, _2, boost::ref(s)));
#ifdef TORRENT_UPNP_LOGGING
if (s.error_code != -1)
{
m_log << time_now_string()
<< " <== got error message: " << s.error_code << std::endl;
std::stringstream msg;
msg << "error while adding port map, code: " << s.error_code;
log(msg.str());
}
#endif
mapping_t& m = d.mapping[mapping];
@ -1074,11 +1025,9 @@ void upnp::on_upnp_map_response(error_code const& e
return_error(mapping, s.error_code);
}
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== map response: " << std::string(p.get_body().begin, p.get_body().end)
<< std::endl;
#endif
std::stringstream msg;
msg << "map response: " << std::string(p.get_body().begin, p.get_body().end);
log(msg.str());
if (s.error_code == -1)
{
@ -1138,32 +1087,27 @@ void upnp::on_upnp_unmap_response(error_code const& e
if (e && e != asio::error::eof)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== error while deleting portmap: " << e.message() << std::endl;
#endif
} else if (!p.header_finished())
std::stringstream msg;
msg << "error while deleting portmap: " << e.message();
log(msg.str());
}
else if (!p.header_finished())
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== error while deleting portmap: incomplete http message" << std::endl;
#endif
std::stringstream msg;
msg << "error while deleting portmap: incomplete http message";
log(msg.str());
}
else if (p.status_code() != 200)
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== error while deleting portmap: " << p.message() << std::endl;
#endif
std::stringstream msg;
msg << "error while deleting portmap: " << p.message();
log(msg.str());
}
else
{
#ifdef TORRENT_UPNP_LOGGING
m_log << time_now_string()
<< " <== unmap response: " << std::string(p.get_body().begin, p.get_body().end)
<< std::endl;
#endif
std::stringstream msg;
msg << "unmap response: " << std::string(p.get_body().begin, p.get_body().end);
log(msg.str());
}
d.mapping[mapping].protocol = none;