supports turning on and off upnp, natpmp and lsd. All ofthem are now off by default

This commit is contained in:
Arvid Norberg 2007-05-31 00:21:54 +00:00
parent b385ed6064
commit 31247c046a
7 changed files with 157 additions and 26 deletions

View File

@ -10,6 +10,7 @@ Magnus Jonsson
Daniel Wallin Daniel Wallin
Cory Nelson Cory Nelson
Stas Khirman Stas Khirman
Ryan Norton
Building and maintainance of the autotools scripts: Building and maintainance of the autotools scripts:
Michael Wojciechowski Michael Wojciechowski

View File

@ -640,6 +640,12 @@ int main(int ac, char* av[])
// monitor when they're not in the directory anymore. // monitor when they're not in the directory anymore.
handles_t handles; handles_t handles;
session ses; session ses;
// UPnP port mapping
ses.start_upnp();
// NAT-PMP port mapping
ses.start_natpmp();
// Local service discovery (finds peers on the local network)
ses.start_lsd();
ses.add_extension(&create_metadata_plugin); ses.add_extension(&create_metadata_plugin);
ses.add_extension(&create_ut_pex_plugin); ses.add_extension(&create_ut_pex_plugin);

View File

@ -299,6 +299,14 @@ namespace libtorrent
{ return m_dht_proxy; } { return m_dht_proxy; }
#endif #endif
void start_lsd();
void start_natpmp();
void start_upnp();
void stop_lsd();
void stop_natpmp();
void stop_upnp();
// handles delayed alerts // handles delayed alerts
alert_manager m_alerts; alert_manager m_alerts;
@ -417,9 +425,9 @@ namespace libtorrent
// but for the udp port used by the DHT. // but for the udp port used by the DHT.
int m_external_udp_port; int m_external_udp_port;
#endif #endif
natpmp m_natpmp; boost::shared_ptr<natpmp> m_natpmp;
upnp m_upnp; boost::shared_ptr<upnp> m_upnp;
lsd m_lsd; boost::shared_ptr<lsd> m_lsd;
// the timer used to fire the second_tick // the timer used to fire the second_tick
deadline_timer m_timer; deadline_timer m_timer;

View File

@ -244,6 +244,16 @@ namespace libtorrent
connection_queue& get_connection_queue(); connection_queue& get_connection_queue();
// starts/stops UPnP, NATPMP or LSD port mappers
// they are stopped by default
void start_lsd();
void start_natpmp();
void start_upnp();
void stop_lsd();
void stop_natpmp();
void stop_upnp();
// Resource management used for global limits. // Resource management used for global limits.
resource_request m_ul_bandwidth_quota; resource_request m_ul_bandwidth_quota;
resource_request m_dl_bandwidth_quota; resource_request m_dl_bandwidth_quota;

View File

@ -360,6 +360,36 @@ namespace libtorrent
m_impl->set_severity_level(s); m_impl->set_severity_level(s);
} }
void session::start_lsd()
{
m_impl->start_lsd();
}
void session::start_natpmp()
{
m_impl->start_natpmp();
}
void session::start_upnp()
{
m_impl->start_upnp();
}
void session::stop_lsd()
{
m_impl->stop_lsd();
}
void session::stop_natpmp()
{
m_impl->stop_natpmp();
}
void session::stop_upnp()
{
m_impl->stop_upnp();
}
connection_queue& session::get_connection_queue() connection_queue& session::get_connection_queue()
{ {
return m_impl->m_half_open; return m_impl->m_half_open;

View File

@ -494,13 +494,6 @@ namespace libtorrent { namespace detail
, m_dht_same_port(true) , m_dht_same_port(true)
, m_external_udp_port(0) , m_external_udp_port(0)
#endif #endif
, m_natpmp(m_io_service, m_listen_interface.address()
, bind(&session_impl::on_port_mapping, this, _1, _2, _3))
, m_upnp(m_io_service, m_half_open, m_listen_interface.address()
, m_settings.user_agent
, bind(&session_impl::on_port_mapping, this, _1, _2, _3))
, m_lsd(m_io_service, m_listen_interface.address()
, bind(&session_impl::on_lsd_peer, this, _1, _2))
, m_timer(m_io_service) , m_timer(m_io_service)
, m_next_connect_torrent(0) , m_next_connect_torrent(0)
, m_checker_impl(*this) , m_checker_impl(*this)
@ -1081,8 +1074,10 @@ namespace libtorrent { namespace detail
{ {
session_impl::mutex_t::scoped_lock l(m_mutex); session_impl::mutex_t::scoped_lock l(m_mutex);
open_listen_port(); open_listen_port();
m_natpmp.set_mappings(m_listen_interface.port(), 0); if (m_natpmp.get())
m_upnp.set_mappings(m_listen_interface.port(), 0); m_natpmp->set_mappings(m_listen_interface.port(), 0);
if (m_upnp.get())
m_upnp->set_mappings(m_listen_interface.port(), 0);
} }
ptime timer = time_now(); ptime timer = time_now();
@ -1107,8 +1102,10 @@ namespace libtorrent { namespace detail
deadline_timer tracker_timer(m_io_service); deadline_timer tracker_timer(m_io_service);
// this will remove the port mappings // this will remove the port mappings
m_natpmp.close(); if (m_natpmp.get())
m_upnp.close(); m_natpmp->close();
if (m_upnp.get())
m_upnp->close();
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
(*m_logger) << time_now_string() << " locking mutex\n"; (*m_logger) << time_now_string() << " locking mutex\n";
@ -1518,13 +1515,18 @@ namespace libtorrent { namespace detail
if (new_listen_address) if (new_listen_address)
{ {
m_natpmp.rebind(new_interface.address()); if (m_natpmp.get())
m_upnp.rebind(new_interface.address()); m_natpmp->rebind(new_interface.address());
m_lsd.rebind(new_interface.address()); if (m_upnp.get())
m_upnp->rebind(new_interface.address());
if (m_lsd.get())
m_lsd->rebind(new_interface.address());
} }
m_natpmp.set_mappings(m_listen_interface.port(), 0); if (m_natpmp.get())
m_upnp.set_mappings(m_listen_interface.port(), 0); m_natpmp->set_mappings(m_listen_interface.port(), 0);
if (m_upnp.get())
m_upnp->set_mappings(m_listen_interface.port(), 0);
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
if ((new_listen_address || m_dht_same_port) && m_dht) if ((new_listen_address || m_dht_same_port) && m_dht)
@ -1534,8 +1536,10 @@ namespace libtorrent { namespace detail
// the listen interface changed, rebind the dht listen socket as well // the listen interface changed, rebind the dht listen socket as well
m_dht->rebind(new_interface.address() m_dht->rebind(new_interface.address()
, m_dht_settings.service_port); , m_dht_settings.service_port);
m_natpmp.set_mappings(0, m_dht_settings.service_port); if (m_natpmp.get())
m_upnp.set_mappings(0, m_dht_settings.service_port); m_natpmp->set_mappings(0, m_dht_settings.service_port);
if (m_upnp.get())
m_upnp->set_mappings(0, m_dht_settings.service_port);
} }
#endif #endif
@ -1557,7 +1561,8 @@ namespace libtorrent { namespace detail
{ {
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
// use internal listen port for local peers // use internal listen port for local peers
m_lsd.announce(ih, m_listen_interface.port()); if (m_lsd.get())
m_lsd->announce(ih, m_listen_interface.port());
} }
void session_impl::on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih) void session_impl::on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih)
@ -1677,8 +1682,10 @@ namespace libtorrent { namespace detail
m_dht_settings.service_port = m_listen_interface.port(); m_dht_settings.service_port = m_listen_interface.port();
} }
m_external_udp_port = m_dht_settings.service_port; m_external_udp_port = m_dht_settings.service_port;
m_natpmp.set_mappings(0, m_dht_settings.service_port); if (m_natpmp.get())
m_upnp.set_mappings(0, m_dht_settings.service_port); m_natpmp->set_mappings(0, m_dht_settings.service_port);
if (m_upnp.get())
m_upnp->set_mappings(0, m_dht_settings.service_port);
m_dht = new dht::dht_tracker(m_io_service m_dht = new dht::dht_tracker(m_io_service
, m_dht_settings, m_listen_interface.address() , m_dht_settings, m_listen_interface.address()
, startup_state); , startup_state);
@ -1708,8 +1715,10 @@ namespace libtorrent { namespace detail
{ {
m_dht->rebind(m_listen_interface.address() m_dht->rebind(m_listen_interface.address()
, settings.service_port); , settings.service_port);
m_natpmp.set_mappings(0, m_dht_settings.service_port); if (m_natpmp.get())
m_upnp.set_mappings(0, m_dht_settings.service_port); m_natpmp->set_mappings(0, m_dht_settings.service_port);
if (m_upnp.get())
m_upnp->set_mappings(0, m_dht_settings.service_port);
m_external_udp_port = settings.service_port; m_external_udp_port = settings.service_port;
} }
m_dht_settings = settings; m_dht_settings = settings;
@ -1883,6 +1892,69 @@ namespace libtorrent { namespace detail
return m_dl_bandwidth_manager.throttle(); return m_dl_bandwidth_manager.throttle();
} }
void session_impl::start_lsd()
{
mutex_t::scoped_lock l(m_mutex);
m_lsd.reset(new lsd(m_io_service
, m_listen_interface.address()
, bind(&session_impl::on_lsd_peer, this, _1, _2)));
}
void session_impl::start_natpmp()
{
mutex_t::scoped_lock l(m_mutex);
m_natpmp.reset(new natpmp(m_io_service
, m_listen_interface.address()
, bind(&session_impl::on_port_mapping
, this, _1, _2, _3)));
m_natpmp->set_mappings(m_listen_interface.port(),
#ifndef TORRENT_DISABLE_DHT
m_dht ? m_dht_settings.service_port :
#endif
0);
}
void session_impl::start_upnp()
{
mutex_t::scoped_lock l(m_mutex);
m_upnp.reset(new upnp(m_io_service, m_half_open
, m_listen_interface.address()
, m_settings.user_agent
, bind(&session_impl::on_port_mapping
, this, _1, _2, _3)));
m_upnp->set_mappings(m_listen_interface.port(),
#ifndef TORRENT_DISABLE_DHT
m_dht ? m_dht_settings.service_port :
#endif
0);
}
void session_impl::stop_lsd()
{
mutex_t::scoped_lock l(m_mutex);
m_lsd.reset();
}
void session_impl::stop_natpmp()
{
mutex_t::scoped_lock l(m_mutex);
if (m_natpmp.get())
m_natpmp->close();
m_natpmp.reset();
}
void session_impl::stop_upnp()
{
mutex_t::scoped_lock l(m_mutex);
if (m_upnp.get())
m_upnp->close();
m_upnp.reset();
}
#ifndef NDEBUG #ifndef NDEBUG
void session_impl::check_invariant(const char *place) void session_impl::check_invariant(const char *place)
{ {

View File

@ -174,7 +174,9 @@ namespace libtorrent
, m_resolve_countries(false) , m_resolve_countries(false)
#endif #endif
, m_announce_timer(ses.m_io_service) , m_announce_timer(ses.m_io_service)
#ifndef TORRENT_DISABLE_DHT
, m_last_dht_announce(time_now() - minutes(15)) , m_last_dht_announce(time_now() - minutes(15))
#endif
, m_policy() , m_policy()
, m_ses(ses) , m_ses(ses)
, m_checker(checker) , m_checker(checker)
@ -245,7 +247,9 @@ namespace libtorrent
, m_resolve_countries(false) , m_resolve_countries(false)
#endif #endif
, m_announce_timer(ses.m_io_service) , m_announce_timer(ses.m_io_service)
#ifndef TORRENT_DISABLE_DHT
, m_last_dht_announce(time_now() - minutes(15)) , m_last_dht_announce(time_now() - minutes(15))
#endif
, m_policy() , m_policy()
, m_ses(ses) , m_ses(ses)
, m_checker(checker) , m_checker(checker)