Browse Source

initialize libtorrent session with external ip set

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
71e0da6705
  1. 3
      libtorrent/include/libtorrent/aux_/session_impl.hpp
  2. 8
      libtorrent/include/libtorrent/session.hpp
  3. 4
      libtorrent/src/session.cpp
  4. 5
      libtorrent/src/session_impl.cpp
  5. 32
      src/init.cpp

3
libtorrent/include/libtorrent/aux_/session_impl.hpp

@ -215,7 +215,8 @@ namespace libtorrent @@ -215,7 +215,8 @@ namespace libtorrent
std::pair<int, int> listen_port_range
, fingerprint const& cl_fprint
, char const* listen_interface
, boost::uint32_t alert_mask);
, boost::uint32_t alert_mask
, char const* ext_ip);
virtual ~session_impl();
void update_dht_announce_interval();
void init();

8
libtorrent/include/libtorrent/session.hpp

@ -139,10 +139,11 @@ namespace libtorrent @@ -139,10 +139,11 @@ namespace libtorrent
, LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0)
, int flags = start_default_features | add_default_plugins
, boost::uint32_t alert_mask = alert::error_notification
, char const* ext_ip = NULL
TORRENT_LOGPATH_ARG_DEFAULT)
{
TORRENT_CFG();
init(std::make_pair(0, 0), "0.0.0.0", print, alert_mask);
init(std::make_pair(0, 0), "0.0.0.0", print, alert_mask, ext_ip);
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
set_log_path(logpath);
#endif
@ -153,12 +154,13 @@ namespace libtorrent @@ -153,12 +154,13 @@ namespace libtorrent
, char const* listen_interface = "0.0.0.0"
, int flags = start_default_features | add_default_plugins
, int alert_mask = alert::error_notification
, char const* ext_ip = NULL
TORRENT_LOGPATH_ARG_DEFAULT)
{
TORRENT_CFG();
TORRENT_ASSERT(listen_port_range.first > 0);
TORRENT_ASSERT(listen_port_range.first < listen_port_range.second);
init(listen_port_range, listen_interface, print, alert_mask);
init(listen_port_range, listen_interface, print, alert_mask, ext_ip);
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
set_log_path(logpath);
#endif
@ -851,7 +853,7 @@ namespace libtorrent @@ -851,7 +853,7 @@ namespace libtorrent
private:
void init(std::pair<int, int> listen_range, char const* listen_interface
, fingerprint const& id, boost::uint32_t alert_mask);
, fingerprint const& id, boost::uint32_t alert_mask, char const* ext_ip);
void set_log_path(std::string const& p);
void start(int flags);

4
libtorrent/src/session.cpp

@ -398,7 +398,7 @@ namespace libtorrent @@ -398,7 +398,7 @@ namespace libtorrent
#endif
void session::init(std::pair<int, int> listen_range, char const* listen_interface
, fingerprint const& id, boost::uint32_t alert_mask)
, fingerprint const& id, boost::uint32_t alert_mask, char const* ext_ip)
{
#if defined _MSC_VER && defined TORRENT_DEBUG
// workaround for microsofts
@ -407,7 +407,7 @@ namespace libtorrent @@ -407,7 +407,7 @@ namespace libtorrent
::_set_se_translator(straight_to_debugger);
#endif
m_impl.reset(new session_impl(listen_range, id, listen_interface, alert_mask));
m_impl.reset(new session_impl(listen_range, id, listen_interface, alert_mask, ext_ip));
#ifdef TORRENT_MEMDEBUG
start_malloc_debug();

5
libtorrent/src/session_impl.cpp

@ -597,6 +597,7 @@ namespace aux { @@ -597,6 +597,7 @@ namespace aux {
, fingerprint const& cl_fprint
, char const* listen_interface
, boost::uint32_t alert_mask
, char const* ext_ip
)
: m_ipv4_peer_pool(500)
#if TORRENT_USE_IPV6
@ -711,6 +712,10 @@ namespace aux { @@ -711,6 +712,10 @@ namespace aux {
m_listen_interface = tcp::endpoint(address::from_string(listen_interface, ec), listen_port_range.first);
TORRENT_ASSERT_VAL(!ec, ec);
if (ext_ip) {
m_external_ip.cast_vote(address::from_string(ext_ip), source_router, address());
}
// ---- generate a peer id ----
static seed_random_generator seeder;

32
src/init.cpp

@ -961,16 +961,34 @@ bool AppInit2(boost::thread_group& threadGroup) @@ -961,16 +961,34 @@ bool AppInit2(boost::thread_group& threadGroup)
#include "libtorrent/bencode.hpp"
#include "libtorrent/session.hpp"
#define TORRENT_DISABLE_GEO_IP
#include "libtorrent/aux_/session_impl.hpp"
using namespace libtorrent;
static session *ses;
void startSessionTorrent(boost::thread_group& threadGroup)
void ThreadWaitExtIP()
{
RenameThread("wait-extip");
std::string ipStr;
// wait up to 5 seconds for bitcoin to get the external IP
for( int i = 0; i < 10; i++ ) {
const CNetAddr paddrPeer("8.8.8.8");
CAddress addr( GetLocalAddress(&paddrPeer) );
if( addr != CAddress() ) {
ipStr = addr.ToStringIP();
break;
}
MilliSleep(500);
}
error_code ec;
int listen_port = GetListenPort() + 1000;
std::string bind_to_interface = "";
printf("startSessionTorrent port=%d\n", listen_port);
printf("Creating new libtorrent session ext_ip=%s port=%d\n", ipStr.c_str(), listen_port);
ses = new session(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0)
, session::add_default_plugins
@ -978,7 +996,8 @@ void startSessionTorrent(boost::thread_group& threadGroup) @@ -978,7 +996,8 @@ void startSessionTorrent(boost::thread_group& threadGroup)
& ~(alert::dht_notification
+ alert::progress_notification
+ alert::debug_notification
+ alert::stats_notification));
+ alert::stats_notification)
, ipStr.size() ? ipStr.c_str() : NULL );
/*
std::vector<char> in;
@ -1003,3 +1022,10 @@ void startSessionTorrent(boost::thread_group& threadGroup) @@ -1003,3 +1022,10 @@ void startSessionTorrent(boost::thread_group& threadGroup)
printf("libtorrent + dht started\n");
}
void startSessionTorrent(boost::thread_group& threadGroup)
{
printf("startSessionTorrent (waiting for external IP)\n");
threadGroup.create_thread(boost::bind(&ThreadWaitExtIP));
}

Loading…
Cancel
Save